My script below needs to insert a record if it does not exists in my database and write it to a file if it does exist also to write the total counts for each in file.

use strict; use warnings; use DBI; #use Win32::ODBC; use DBD::ODBC; # Global Variables my @statement; # Array of sql statements that will be used for Sql my @data; # Array of table rows read from Sybase my @message; # Array of error messages used for debugging Sql #my $destination = /errorlog.txt my $errcounter = 0; my $succounter = 0; my $UniKey; # SQL Connection my $dbh = DBI->connect( "dbi:ODBC:RETQC_SQL", "sa", "", {RaiseError => 1, PrintError => 1, AutoCommit => 0} ) or die "Unable to connect: ";#. $DBI::errstr . "\n"; # Sybase Connection my $dbh2 = DBI->connect( "dbi:ODBC:BPSA", "", "", {RaiseError => 1, PrintError => 1, AutoCommit => 0} ) or die "Unable to connect: ";#. $DBI::errstr . "\n"; open( OUTFILE, "> c:/errorlog.txt" ) or die "Couldn't open file for re +ading.$!\n"; #Create the sql statement for Sybase and execute it $sth = $dbh2->prepare("SELECT (dbo.tbookkeeping_trans.branch_cd + dbo. +tbookkeeping_trans.account_cd)As AccountNo, 'TRI'as TransferFlag,'NCT' as TransferType, 'C' as RejectType, dbo.tbo +okkeeping_trans.security_adp_nbr as AdpNumber, dbo.tbookkeeping_trans.share_trans_qty as Shares,dbo.tbookkeeping_tra +ns.processing_dt 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) + VALUES ( ?, ?, ?, ?, ?, ?, ? )"); eval { while((@data) = $sth->fetchrow_array) { my $ncount = 0; $ncount = "Select Count(*) From ClearTransferData Where AccountNo += $data[0] and TransferFlag = $data[1] and TransferType = $data[2] a +nd RejectType = $data[3] and AdpNumber = $data[4] and Shares = $data[ +5] and ProcessingDate = $data[6] "; If $ncount gt 0; { print OUTFILE @data; my $errcounter++; } $insh->execute(@data); my$succounter++; } }; if($@) { print STDERR "The insert died: $@"; } $dbh2->commit(); $dbh->commit(); #Close the sql DB Connection $dbh->disconnect; #Close the Sybase DB connection $sth->finish; $dbh2->disconnect;

edited: Wed May 14 20:54:23 2003 by jeffa - code tags and a readmore


In reply to Reading from databse and writing to file. 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.