http://www.perlmonks.org?node_id=385711


in reply to prepare statement within DBI

Whatever your database driver accepts as a valid SQL statement is fine as far as the DBI is concerned. Thus, if Oracle accepts a query like "SELECT COUNT(*) from t1; SELECT COUNT(*) from t2;" so you can do it.

If that is not the case, you can force that behavior with some SQL features:

my $query = qq{ select "t1" as t, count(*) as c from table_one UNION ALL select "t2", count(*) from table_two }; # Gives # +----+---+ # | t | c | # +----+---+ # | t1 | 4 | # | t2 | 2 | # +----+---+ my $table_status = $dbh->selectall_arrayref($query, {Slice => {}} ); # $table_status = [ # { t => 't1', c => 4 }, # { t => 't2', c => 2 } # ];

Or:

my $query = qq{ SELECT ( select count(*) from table_one ) AS t1, ( select count(*) from table_two ) AS t2; }; # Gives # +----+----+ # | t1 | t2 | # +----+----+ # | 4 | 2 | # +----+----+ my @table_status = $dbh->selectrow_array($query); # @table_status = ( 4, 2 );

I would personally recommend against this, since I prefer to have better control on my SQL statements. However, if you know what you're doing, it's up to you to decide.

 _  _ _  _  
(_|| | |(_|><
 _|   

Replies are listed 'Best First'.
Re^2: prepare statement within DBI
by Velaki (Chaplain) on Aug 25, 2004 at 18:09 UTC

    Additionally, consider looking at the results of your database's query optimizer and analyzer to see what the trade-off will be in creating more complex SQL queries to offset the cost on the perl side in making multiple calls.

    Maybe a stored proc is in order?

    Thoughts,
    -v
    "Perl. There is no substitute."