punch_card_don has asked for the wisdom of the Perl Monks concerning the following question:

Mordant Monks,

I need to do repeated insertions over many tables:

for $t (0 .. 1000) { $table_name = $tables[$t]; $theValue = $values[$t]; $sql = "INSERT INTO r".$table_name." VALUES ($theValue)"; $std = $dbh->prepare($sql) or die("Could not prepare! At table = +$table_name because " . $dbh->errstr); $std->execute() or die("Could not execute!" . $dbh->errstr); }
This is proving very slow, partly, I suspect, because of the new prepare for every insert. If I could use placeholders for both $table_name and $theValue, I could probably save a lot of time. Is there some way to do this, or can placeholders ony be used for the values?

If I'm bothering the monastery with this, it's because I've already tried thihngs along the lines of

$sql = "INSERT INTO ? VALUES (?)"; $std = $dbh->prepare($sql) or die("Could not prepare! At table = $tabl +e_name because " . $dbh->errstr); for $t (0 .. 1000) { $table_name = r.$tables[$t]; $theValue = $values[$t]; $std->execute($table_name , $theValue) or die("Could not execute!" . $ +dbh->errstr); }
with dismal results.

Thanks.

Replies are listed 'Best First'.
Re: DBI: Use placeholders for table name?
by perrin (Chancellor) on Dec 04, 2004 at 01:54 UTC
    You can't use placeholders for table names. Just prepare your query once per table with a placeholder for the value. Using prepare_cached() may make it simpler.
Re: DBI: Use placeholders for table name?
by Zaxo (Archbishop) on Dec 04, 2004 at 01:54 UTC

    I don't know of a RDBMS and DBD driver which accepts placeholders in a structural slot like the table name. Perhaps a redesign of the database and its usage would be helpful.

    After Compline,
    Zaxo

      I don't know of a RDBMS and DBD driver which accepts placeholders in a structural slot like the table name.

      Last time I looked, several of the drivers for databases that don't support prepare would allow you to use placeholders anywhere in the query, since the bound variables are expanded by the driver at execute time. This "feature" allowed people to mislead themselves into thinking that binding table names is possible in the general case.

Re: DBI: Use placeholders for table name?
by punch_card_don (Curate) on Dec 04, 2004 at 02:04 UTC
    Ya, I kinda knew as much - clutching at straws late on a friday night.

    Thanks guys.

Re: DBI: Use placeholders for table name?
by Juerd (Abbot) on Dec 05, 2004 at 11:25 UTC

    Sounds to me like you really need just one table with an additional column. Tables are 2D. Don't use additional tables to get 3D.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }