I could combine the two sql statements into one statement

Well, you could with a union:

select 't1', count(*) from t1 union select 't2', count(*) from t2

... but you must add something to each select (here, the name of the table as a string) to what is returned in order to guarantee that you can determine which count(*) corresponds to which table. You never know which order they'll come back in. But now you have just about as much make-work code to pick the apart the result set and get what you want.

My advice would be to encapsulate it in a sub and use cached prepares.

my $t1_count = table_row_count( $dbh, 'table1' ); my $t2_count = table_row_count( $dbh, 'table2' ); sub table_row_count { my $db = shift; my $table = shift; my $sth = $db->prepare_cached( "select count(*) from $table" ); $sth->execute(); return $sth->fetchrow_array; }

Error checking is left as an exercise to the reader.

- another intruder with the mooring of the heat of the Perl


In reply to Re: prepare statement within DBI by grinder
in thread prepare statement within DBI by Paulster2

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.