I have the need to execute a query in MySQL using Perl and DBI and insert the results in another table. The second table is created on the fly to define the same number of fields that the original query returned, plus one more field.
I have no way to know in advance the first query or how many fields it will return. So I'm doing this very ugly thing (summarized):
# Execute the received query
my $sth = $dbh->prepare($received_sql);
$sth->execute;
# Get the fields names and create the new table
my @fields = @{$sth->{NAME}};
my $create_table = "...some sql and a map from @fields...";
# Start getting the results from the query one by one just to insert them into the new table
while (my @record = $sth->fetchrow_array) {
my $sql = "insert into `new_table` (";
$sql .= join ', ', map { "`$_`" } @fields;
$sql .= ") values (";
$sql .= join ', ', map { '?' } @fields;
$sql .= ')';
my $insert = $dbh->prepare($sql)
$insert->execute(@record);
}
It would be perfect if I could just do this:
insert into `new_table` (...) select (...) from ...
but, I need to know the names of the fields returned from the first query and create the corresponding table before inserting data into it.
So, I've been wondering if, since $sth is an actual prepared statement... is there anyway to use it to tell MySQL to insert the results of that statement into another table, without making it pass record by record to Perl?
Thanks in advance for your comments.
Francisco