in reply to Re: DBI - Table names & placeholders
in thread DBI - Table names & placeholders
If you're worried about unusual table namesWell, no - that's not my problem.
Imagine that I have two sets of tables, one a set of transactional records (eg, sales records), and the other a set of consolidated tables for reporting. So I have a Sales table with attributes for Date/Time, ItemID & Price.
Then I have a set of tables that look like Sales201003, Sales201004, Sales201005, etc - a separate table for each year/month combination. (This is not the way I would design it - but this is how it is and so I need to deal with it).
The SalesYYYYMM tables have one row per day, per item, along with attributes for the total number sold, and total revenue.
So I have code that selects records from the Sales table, and then inserts summarised data into the appropriate SalesYYYYMM tables. The code I would _like_ to be able to write would look something like this:
But the above won't work because I can't use a placeholder for a table name.# Some code here which selects data from Sales table # ... # ... # then... my @sales_inserts; my $insert_sql = 'INSERT INTO ? (ItemID, Day, Revenue, Count) VALUES ( +?, ?, ?, ?); my $sales_insert_dbq = $sales_dbh->prepare($insert_sql); while (my @data = $dbq->fetchrow_array) { my ($year, $month, $day, $itemid, $revenue, $count) = @data; my $sales_table = "Sales$year$month"; my @insert_values = ($sales_table, $day, $itemid, $revenue, $count +); push @sales_inserts, [@insert_values]; } eval { for my $i (0 .. $#sales_inserts) { $sales_insert_dbq->execute(@{$sales_inserts[$i]}); } $sales_dbh->commit; }; if ($@) { # Error handling, rollback, etc }
Oh, and please ignore the fact that it's horrible data base design, and that calculated data is being written back into the data base. As I said, it's a contrived example that helps to describe my problem.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: DBI - Table names & placeholders
by ikegami (Patriarch) on Apr 24, 2010 at 09:04 UTC | |
by McDarren (Abbot) on Apr 26, 2010 at 06:24 UTC | |
by ikegami (Patriarch) on Apr 26, 2010 at 07:08 UTC | |
by McDarren (Abbot) on Apr 26, 2010 at 11:13 UTC | |
by ikegami (Patriarch) on Apr 26, 2010 at 16:04 UTC | |
| |
|
Re^3: DBI - Table names & placeholders
by ig (Vicar) on Apr 24, 2010 at 06:33 UTC | |
|
Re^3: DBI - Table names & placeholders
by Anonymous Monk on Dec 03, 2012 at 16:40 UTC | |
by erix (Prior) on Dec 03, 2012 at 17:19 UTC |