Hello Monks,

I'm trying to put logic into perl coding but not able to do so for sometime now.

What I'm trying to do is

1. Open csv file

2. Insert line into Oracle table

3. If any error then

3.1 Rollback all inserts of the file

3.2 Update status table with FAILURE

3.3 Move to next file

4. If all inserts completed then

4.1 Update status table with SUCCESS

5. Commit

Could some expert monks help me out sorting this logic?
use warnings; use strict; use Text::CSV; use DBD::Oracle; my $exitStatus = 0; # Connect to database and disable autocommit my $dbAgent = OracleAgent->dbLogin(dbName); $dbAgent->connect(); $dbAgent-> setAutoCommit(0); #@csvFile holds all csv files list to load in db foreach my $file (@csvFile) { chomp ($item); &insertRecords($file); } $logFile-> ("Exit status"); #---------------- sub insertRecords { my $csv; my $fileToInsert = shift; my $row; my $SQL; my $sth; my $dbh; my $rc; open my $fh, "<", $fileToInsert or die "$filetoInsert: $!" $logFile -> ("Working under $file") while ($row = $csv->getline ($fh)) { $SQL1 = "Insert into TAB1 (sample_date, server, first, n1, n2) values (?,?,?,?,?)"; $sth = prepare($SQL1) $sth -> execute($row[0], $row[1], $row[2], $row[3], $row[4]) || { logFile($DBI::errstr); $dbAgent->rollback; $rc=0; #if error in insert then update status table $SQL3 = "Insert into STATUS (sample_date, server +, cvs_file, row_count, status) values (sysdate,$row[1],$file,$rc,'FAI +LURE')"; $sth = executeQuery($SQL3); } else { #if no error in insert then update status table an +d commit everything $SQL2 = "Insert into STATUS (sample_date, server, + cvs_file, row_count, status) values (sysdate,$row[1],$file,$rc,'SUCCESS')"; $sth = executeQuery($SQL2); $rc++; } close $fh; $dbAgent->commit;

In reply to Perl oracle insert error handling by homer4all

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.