If you're trying to group those 4 queries into a single one, then I would do build the query programmatically. There are a couple things you have to worry about, not all the columns are being initialized in every insert, and it would be very easy to assign a value to the wrong field.
I would therefore create a list of hashes that contain field name -> value pairs for each record you're wanting to create. Then compile the list of all possible field names and build the single insert statement. The following code should work for the sample code you provided, but obviously this is untested
my %common = ( source => "$dir-$filename", substatus => $status, original => $line, imported => $datetime, ); my @records = ( { contract => $data[1], invoice => $data[2], %common, }, { transaction => $data[1], contract => $data[2], amount => $data[3], applied => $data[4], account => $data[7], %common, }, { contract => $data[1], amount => $data[2], tax => $data[3], applied => $data[5], nameonpayment => $data[6], invoice => $data[7], %common, }, { nameonpayment => $data[1], account => $data[2], amount => $data[4], applied => $data[5], invoice => $data[6], %common, }, ); my @fields = do { my %seen; grep {! $seen{$_}++} map {keys %$_} @records; }; my $fields = join ',', @fields; my $placeholders = '(' . join(',', ('?') x @fields). ')'; my $all_placeholders = join ',', ($placeholders) x @records; my @values = map {@{$_}{@fields}} @records; $dbh->do("insert into batchpay ($fields) values $all_placeholders", {} +, @values) or die $dbh->errstr;
In reply to Re^3: Using variables in a DBI do queriy
by wind
in thread Using variables in a DBI do queriy
by vendion
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |