Loading ...
Sorry, an error occurred while loading the content.

Error handling

Expand Messages
  • davidbsamuels
    I wrote this script that reads from one database and inserts into another one. What I need to do is to add error handling to the script. I would like for the
    Message 1 of 2 , May 12, 2003
    • 0 Attachment
      I wrote this script that reads from one database and inserts into
      another one. What I need to do is to add error handling to the
      script. I would like for the script to do is to check if the record
      already exists and if it does then write to error log otherwise
      insert it. I don't know how to write the code. I would appreciate
      if
      somebody could help me. Thanks in advance.

      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


      # 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";




      #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) {
      $insh->execute(@data);
      }
      };
      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;
    • gregmeckes
      Couldn t you try something like: $insh- execute(@data) || print STDERR The insert died: $@ ; Greg ... Sql ... dbo. ... dbo.tbo ... Shares,dbo.tbookkeeping_tra
      Message 2 of 2 , May 12, 2003
      • 0 Attachment
        Couldn't you try something like:
        $insh->execute(@data) || print STDERR "The insert died: $@";

        Greg

        --- In perlforbeginners@yahoogroups.com, davidbsamuels
        <no_reply@y...> wrote:
        > I wrote this script that reads from one database and inserts into
        > another one. What I need to do is to add error handling to the
        > script. I would like for the script to do is to check if the record
        > already exists and if it does then write to error log otherwise
        > insert it. I don't know how to write the code. I would appreciate
        > if
        > somebody could help me. Thanks in advance.
        >
        > 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
        >
        >
        > # 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";
        >
        >
        >
        >
        > #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) {
        > $insh->execute(@data);
        > }
        > };
        > 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;
      Your message has been successfully submitted and would be delivered to recipients shortly.