in reply to Re: DBI query where table name is a variable
in thread DBI query where table name is a variable

Though you probably can also use placeholders
my $sth = $dbh->prepare("select column from ? where column = ?"); my $table = 'foo'; my $value = 'bar'; $sth->execute($table, $value);

just another cpan module author

Replies are listed 'Best First'.
Re^3: DBI query where table name is a variable
by Krambambuli (Curate) on Oct 23, 2007 at 12:49 UTC
    Hmmmm... Take care when using placeholders as tablenames; you'll better check if that works with your DB and should avoid it even if it works if portability matters.

    The DBI documentation mentions that
    ---
    With most drivers, placeholders can't be used for any element of a sta +tement that would prevent the database server from validating the sta +tement and creating a query execution plan for it. For example: "SELECT name, age FROM ?" # wrong (will probably fail +) "SELECT name, ? FROM people" # wrong (but may not 'fail' +)

    Krambambuli
    ---
    enjoying Mark Jason Dominus' Higher-Order Perl
Re^3: DBI query where table name is a variable
by andreas1234567 (Vicar) on Oct 23, 2007 at 12:48 UTC
    I strongly suspect one cannot use placeholders for table names due to quoting. The resulting SQL with placeholders will be syntactically incorrect:
    select col1 from 'foo' where col1 = '3'
    A complete sample (trace output filtered for readability):
    --
    Andreas