SQLMan has asked for the wisdom of the Perl Monks concerning the following question:

Below is part of my code. What I am trying to do is select from a database and assign to
one of the fields the DATE, Field1, and Line number. I am increasing the line
number inn the while loop but I am preparing the select outside the loop so my
results are Date, Field1 and 0 for line number. How can I have the select pick up the
incremented line number or is there a better way to do it.

# #Create the sql statement for Sybase and execute it my $sth = $dbh2->prepare("SELECT (dbo.tbookkeeping_trans.branch_cd + d +bo.tbookkeeping_trans.account_cd)As AccountNo, 'TRI'as TransferFlag,'NCT' as TransferType, 'C' as RejectType, dbo.tbo +okkeeping_trans.security_adp_nbr as pNumber, dbo.tbookkeeping_trans.share_trans_qty as Shares,dbo.tbookkeeping_tra +ns.processing_dt, (dbo.tbookkeeping_trans.branch_cd + dbo.tbookkeeping_trans.account_cd ++'".$datestamp."'+'".$lineno."')As KeyId FROM dbo.tbookkeeping_trans + WHERE (dbo.tbookkeeping_trans.client_nbr='0030' AND dbo.tbookkeeping_t +rans.entry_cd IN ('JNL', 'JRL', 'REC') AND dbo.tbookkeeping_trans.branch_cd >'248') ORDER BY 1"); $sth->execute; my $insh=$dbh->prepare("INSERT INTO ClearTransferData(AccountNo, Trans +ferFlag, TransferType, RejectType, AdpNumber, Shares, ProcessingDate, +KeyId) VALUES ( ?, ?, ?, ?, ?, ?, ? ,?)"); eval { while((@data) = $sth->fetchrow_array) { $lineno++; my $keyId = ("$data[0]".$lineno.$datestamp); print "$keyId\n"; my $sql=("Select Count(*) From ClearTransferData Where AccountNo = +'$data[0]' and TransferFlag = '$data[1]' and TransferType = '$data[2 +]' and RejectType = '$data[3]' and AdpNumber = '$data[4]' and Shares += '$data[5]' and ProcessingDate = '$data[6]' "); my ($ncount) = $dbh->selectrow_array($sql); print "$ncount\n"; if ($ncount > 0) { $errcounter++; print OUTFILE "@data\n"; } else { $insh->execute(@data); $succounter++; } } };

update (broquaint): added <code> tags

Replies are listed 'Best First'.
Re: Counter and select statement
by pfaut (Priest) on May 20, 2003 at 14:12 UTC

    You interpolated $lineno into your SQL statement outside the loop. The value it held at the time was embedded into the SQL statement and was used for all rows returned.

    You could easily create the field where you are using $lineno with perl instead of having the database return it. The data coming from the database is already present in the list of fields - you are just appending $datestamp and $lineno to this. It doesn't make sense to give data to the database just to have the database return it.

    Some other notes...

    You can use placeholders on select statements as well as on inserts. Why do you use it for one and not the other? Placeholders are a much cleaner way to plug data into your SQL than string interpolation.

    Some of the fields in your first select were literals which you just took and plugged into another SQL statement. You could have just coded the SQL statements with those constants.

    my $sth = $dbh2->prepare(<<SQL); SELECT branch_cd + account_cd As AccountNo, security_adp_nbr as pNumber, share_trans_qty as Shares, processing_dt FROM dbo.tbookkeeping_trans WHERE client_nbr='0030' AND entry_cd IN ('JNL', 'JRL', 'REC') AND branch_cd >'248' ORDER BY 1 SQL $sth->execute; my $cnth=$dbh->prepare(<<SQL); Select Count(*) From ClearTransferData Where AccountNo = ? and TransferFlag = 'TRI' and TransferType = 'NCT' and RejectType = 'C' and AdpNumber = ? and Shares = ? and ProcessingDate = ? SQL my $insh=$dbh->prepare(<<SQL); INSERT INTO ClearTransferData(AccountNo, TransferFlag, TransferType, RejectType, AdpNumber, Shares, ProcessingDate,KeyId) VALUES ( ?, 'TRI', 'NCT', 'C', ?, ?, ? ,?) SQL eval { while((@data) = $sth->fetchrow_array) { $lineno++; my $keyId = ("$data[0]".$lineno.$datestamp); print "$keyId\n"; $cnth->execute(@data); my ($ncount) = $cnth->fetchrow_array($sql); my $KeyID = $data[0] . $datestamp . $lineno; print "$ncount\n"; if ($ncount > 0) { $errcounter++; print OUTFILE "@data\n"; } else { $insh->execute(@data,$KeyID); $succounter++; } } };
    90% of every Perl application is already written.
    dragonchild
Re: Counter and select statement
by mpeppler (Vicar) on May 21, 2003 at 13:22 UTC
    pfaut++.

    However, for others who may want to create a select statement with Sybase that numbers each row you can do it like this:

    select row_num identity(5), col1, col2, col3, ... into #tmp from the_table where some_condition select row_num, col1, col2, col3, ... from #tmp order by row
    This creates a temporary table with the first column being auto-generated and incremented for each row.

    Off course it's usually easier to do this as the rows are fetched from the database, but in some cases it's nice to have this option.

    Michael