Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
If you're worried about unusual table names
Well, no - that's not my problem.
The problem is that I will not know the names of the tables until run-time.
Let me give another (once again, contrived) example, that might explain it a bit better.

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:

# 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 }
But the above won't work because I can't use a placeholder for a table name.
I can work around it easily enough by creating a separate statement handle for each YYYYMM combination.
I'm just not sure what the best approach to that would be.

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.

In reply to Re^2: DBI - Table names & placeholders by McDarren
in thread DBI - Table names & placeholders by McDarren

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-06-18 22:43 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.