in reply to Dynamically passing table names to a sql query(DBI)

my $sth = $dbh->prepare( q{SELECT * FROM $table}) || die "Can't prepare statement: $DBI::errstr";

...you have the query single quoted which does not interpolate your $table variable, try this:

my $sth = $dbh->prepare( qq{SELECT * FROM $table}) || die "Can't prepare statement: $DBI::errstr";

...notice the "qq{" which is double quotes and will interpolate $table.

--
hiseldl
What time is it? It's Camel Time!

Replies are listed 'Best First'.
Re^2: Dynamically passing table names to a sql query(DBI)
by adrianh (Chancellor) on Oct 09, 2002 at 19:54 UTC

    If you're paranoid, and not certain that the table name can be interpolated directly, you also might want to consider using quote_identifier to quote the table name for the select.

    Untested code...

    my $quoted_table = $dbh->quote_identifier($table); my $sth = $dbh->prepare(qq{SELECT * FROM $quoted_table}) or die "Can't prepare statement: $DBI::errstr";
Re: Re: Dynamically passing table names to a sql query(DBI)
by Anonymous Monk on Oct 09, 2002 at 15:03 UTC
    Thank you so much for the input, it works fine.