[Omaha.pm] Re: Getting @@ROWCOUNT
Jay Hannah
jay at jays.net
Thu Nov 18 15:29:06 CST 2004
Uhh... You want the number of rows you SELECTed? Typically I use
fetchrow() to walk each row in a cursor-style walk. Like so:
$rowcount = 0;
while (@row = $sth->fetchrow) {
$rowcount++;
# ... do whatever ...
}
In that mode, you don't know $rowcount until you've walked all the data
doing your real work.
Or just change your SQL statement to "select count(*) from..." if you
have no real work to do?
Or slurp the entire results set into Perl memory up front...?
my @rowsref = $sth->fetchall_arrayref;
my $rowcount = @rowsref;
foreach (@rowsref) {
my $rowref = $_;
# ... do whatever ...
}
?
HTH,
j
On Nov 18, 2004, at 7:59 AM, Michael D. Maynard wrote:
> That would work with INSERT. Any thoughts on SELECT queries? The
> select @@ROWCOUNT essentially returns a second record set AFTER all
> the rows have been fetched.
>
> Michael
>
>
> At 08:42 PM 11/17/2004, you wrote:
>
>> On Nov 17, 2004, at 5:03 PM, Michael D. Maynard wrote:
>>> Do you know how to get @@ROWCOUNT from MSSQL for a query with PERL?
>>> DBI rows() is not supported. :-(
>>
>> Hmmm... here's an example of @@identity. I assume @@rowcount would
>> work the same way?
>>
>> my $strsql = "insert into blah....";
>> $strsql .= ' select @@identity';
>> # print "\n\n$strsql\n\n";
>> my $sth = $dbh->prepare($strsql);
>> $sth->execute;
>> my @row = $sth->fetchrow_array;
>> $sth->finish;
>> print "Just inserted new ID '$row[0]'\n";
>>
>> HTH,
>>
>> j
>>
More information about the Omaha-pm
mailing list