geet has asked for the wisdom of the Perl Monks concerning the following question:
I am having problems with the output from a query. I am running perl on Win32, and I am connecting to an MS SQL database using the DBI ODBC driver.
I am doing a query by date. I use bind_columns to work with the results. I use a 'while (fetch())' to write out to a file.
My problem is the first row of my results isn't printed. Everything else prints out just fine. If I dump the array using "print OUTPUT "@rows";" the first row is there, so I know it is coming in my result set.
Is there a limitation on the number of columns I can return? Is there a better way to do this? I am programming and perl newbie.
This is essentially my script:
#!perl use strict; use DBI; my $g_date = '09/23/03'; open (OUTPUT, ">$somefile"); #create a header with column names print OUTPUT "seq,prcdate,frb,account,amount,checknum,tc,disp,run,batc +h,pkt,catcode,deviceid,volid,imagefile,status,typeid,bankId,itemNumbe +r,editStatus,exceptionReasonCode,documentId,postAccount,appCode,bundl +eImageOffset,bundleImageLength,archiveDeviceId,archiveVolId,archiveIm +ageFile,archiveImageOffset,archiveImageLength,userIdAmountEntry,AUX,P +C\n"; my $dbh = DBI ->connect ("DBI:ODBC:mydsn", "user", "password", { Raise +Error => 1, AutoCommit => 0 }); my $sql = "Select * from items where procdate = \'$g_date\' "; my $sth = $dbh->prepare($sql); $sth->execute()or die "\n\n Unable to execute SQL query!\n\n"; my ($seq,$prcdate,$frb,$account,$amount,$check,$tc,$disp,$run,$batch,$ +pkt,$catcode,$deviceid,$volid,$imagefile,$status,$typeid,$bankId,$ite +mNumber,$editStatus,$exceptionReasonCode,$documentId,$postAccount,$ap +pCode,$bundleImageOffset,$bundleImageLength,$archiveDeviceId,$archive +VolId,$archiveImageFile,$archiveImageOffset,$archiveImageLength,$user +IdAmountEntry,$AUX,$PC); my @rows = $sth->fetchrow_array; my $rowcount = @rows; if ($rowcount > 0) # don't do anything when no results { $sth-> bind_columns( undef, \$seq, \$prcdate, \$frb, \$account, + \$amount, \$check, \$tc, \$disp, \$run, \$batch, \$pkt, \$catcode, \ +$deviceid, \$volid, \$imagefile, \$status, \$typeid, \$bankId, \$item +Number, \$editStatus, \$exceptionReasonCode, \$documentId, \$postAcco +unt, \$appCode, \$bundleImageOffset, \$bundleImageLength, \$archiveDe +viceId, \$archiveVolId, \$archiveImageFile, \$archiveImageOffset, \$a +rchiveImageLength, \$userIdAmountEntry, \$AUX, \$PC); while( $sth->fetch() ) { print OUTPUT "$seq,$prcdate,$frb,$account,$amount,$check, +$tc,$disp,$run,$batch,$pkt,$catcode,$deviceid,$volid,$imagefile,$stat +us,$typeid,$bankId,$itemNumber,$editStatus,$exceptionReasonCode,$docu +mentId,$postAccount,$appCode,$bundleImageOffset,$bundleImageLength,$a +rchiveDeviceId,$archiveVolId,$archiveImageFile,$archiveImageOffset,$a +rchiveImageLength,$userIdAmountEntry,$AUX,$PC\n"; } } $dbh->disconnect(); close OUTPUT;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: ODBC DBI Problems
by ikegami (Patriarch) on Dec 02, 2004 at 19:31 UTC | |
by geet (Initiate) on Dec 02, 2004 at 19:50 UTC |