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
-
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.
|