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

I have this script that reads data from one database
and inserts it into another. I need to modify my script to
look at a table in the first database and if the count
is greater than 0 then it should execute the script
but if not then it should sleep for 15 min and try
again. This should keep happeing until the count is
greater than 0. Below is the my script.

#!/usr/bin/perl use strict; use warnings; use DBI; 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 <p> my $destination2 = "/processlog.txt"; my $errcounter = 0; my $succounter = 0; my $runtime = localtime; my( $sec, $min, $hour, $mday, $mon, $year ) = gmtime; my $datestamp = sprintf "%02d%02d%04d", $mon+1, $mday,$year+1900; my $destination = "\/BookErrorlog$datestamp.err"; my $lineno = 0; # SQL Connection my $dbh = DBI->connect( "dbi:ODBC:RETQC_SQL", "", "", {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, ">> $destination" ) or die "Couldn't open file for read +ing.$!\n"; open( PROSFILE, ">> $destination2" ) or die "Couldn't open file for re +ading.$!\n"; # #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 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 dbo.tbookkeeping_tr +ans.branch_cd , dbo.tbookkeeping_trans.account_cd, dbo.tbookkeeping_trans.security_adp_nbr,dbo.tbookkeeping_trans.share_t +rans_qty,dbo.tbookkeeping_trans.processing_dt"); $sth->execute; #print "sth=$sth/n"; # my $insh=$dbh->prepare("INSERT INTO ClearTransferDataTemp(AccountNo, T +ransferFlag, TransferType, RejectType, AdpNumber, Shares, ProcessingD +ate,KeyId) VALUES ( ?, ?, ?, ?, ?, ?, ? ,?)");<p> print OUTFILE "|------------------------------------------------------ +--------|\n"; print OUTFILE "| BOOKKEEPING Process Date $runtime:|\n"; print OUTFILE "|------------------------------------------------------ +--------|\n"; eval { while((@data) = $sth->fetchrow_array) { $lineno++;<p> my $keyId = ("$data[0]".$datestamp.$lineno); #print "$keyId\n"; push (@data,$keyId); #print "$keyId\n"; my $sql=("Select Count(*) From ClearTransferDataTemp Where KeyId = +'".$keyId."'"); my ($ncount) = $dbh->selectrow_array($sql); #print "$ncount\n"; if ($ncount > 0) { $errcounter++; print OUTFILE "@data\n"; } else { $insh->execute(@data); $succounter++; } } }; if($@) { print STDERR "The insert died: $@"; print OUTFILE "The insert died: $@\n"; }<p> print PROSFILE "|----------------------------------------------------- +---------|\n"; print PROSFILE "| BOOKKEEPING Process Date $runtime:|\n"; print PROSFILE "|----------------------------------------------------- +---------|\n"; print PROSFILE "|Records inserted $succounter |\n"; print PROSFILE "|Records written to error log $errcounter |\n"; print PROSFILE "|----------------------------------------------------- +---------|\n"; print OUTFILE "|Records written to error log $errcounter |\n"; print OUTFILE "|------------------------------------------------------ +--------|\n"; $dbh2->commit(); $dbh->commit(); #Close the sql DB Connection $dbh->disconnect; #Close the Sybase DB connection $sth->finish; $dbh2->disconnect;

20030826 Edit by Corion: Fixed formatting

Replies are listed 'Best First'.
Re: Data check or sleep
by benn (Vicar) on Aug 26, 2003 at 12:50 UTC
    In cases like this, where the primary purpose is something straightforward (as stated in the question), I'd prefer to split up the code into 'obvious' subroutines, so that the main block of code looks something like this...
    my $dbh = DBI->connect($connection_string); my $dbh2 = DBI->connect($connection_string2); while (1) { my $count = get_table_count($dbh); do_table_copy($dbh,$dbh2) if $count; sleep(15 * 60); }
    Note that you don't need to sleep *only* if $count == 0 - if you perform the copy, presumably you're not going to want to bother checking again for another 15 minutes.

    Cheers, Ben.

Re: Data check or sleep
by Abigail-II (Bishop) on Aug 26, 2003 at 11:55 UTC
    Uhm, I'm not sure I understand your question. Do you want us to insert a sleep command in that long blob of code? Or don't you know that you use sleep to sleep?

    Abigail

      The data is loaded into database 1 from an outside process. This process does not always run at the same time. So I need the script to tell me if the data was loaded.
        Yes, I fully understand what you want. But what is your question?

        Abigail