in reply to SQL with DBI

my @tables_to_drop = qw(Grab_Addr2 Grab_MCC1 Grab_MCC2); foreach my $table (@tables_to_drop) { $dbh->do("DROP TABLE $table;") or die $dbh->errstr(); }
As CountZero described above it is easier to use a foreach-loop. You don't need a statement-handle to drop tables (see DBI for more details). And you can use the qw-quoting operator to quote the tables in your array. The code above is untested but should be okay.

Replies are listed 'Best First'.
Re^2: SQL with DBI
by sk (Curate) on Jun 26, 2005 at 20:45 UTC
    Extending on what neniro and CZ have said-

    you can also do something like this

     my $tables = join (',',@tables_to_drop;)

    and use it in the drop statement  $dbh->do("DROP TABLE $tables;") or die dbh->errstr;

    I am assuming your SQL engine allows multiple table drops when they separated by commas. It works for me in MySQL

    Also note that using the $variable inside the query is sometimes considered to be dangerous esp if it is a user input. http://www.perl.com/pub/a/1999/10/DBI.html

    cheers

    SK

Re^2: SQL with DBI
by Anonymous Monk on Jun 26, 2005 at 19:15 UTC
    Works great thanks guys....