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

In reply to Re: Counter and select statement by pfaut
in thread Counter and select statement by SQLMan

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.