Hello Perl Monks. I have a script where I am calling stored procedures. The stored procedure needs minimum one parameter to return a value. There are other optional values that can be passed to the stored procedure in which scenario the behavior will change. So, I am confused as how to prepare the statements and how to execute.
use DBI qw(:sql_types); use strict; require "DBUtil.pl"; require "BatchEnv.pl"; my $procName = shift; my $cycleDate = shift; my $frequency = shift; my $finRepId = shift; my $fromDate = shift; my $toDate = shift; my $showPendingPolicies = shift; my $strConnectionString; my $dsn; my $myDSN; my $user; my $password; my $dbh; my $procedure; my $function; my $retCode; my $retMsg; if($procName eq ''){ print "Usage: ExecuteProc.pl [Procedure Name] -o[CycleDate] -o[Fre +quency] -o[Fromdate] -o[ToDate] -o[FinRepId] -o[ShowPendingPolicies]" +; exit(); } print ("In the script\n"); # Open Database connection $strConnectionString = &GetConnectionString; $_=$strConnectionString; if(/data source=([0-9A-Za-z]*;)user id=([0-9A-Za-z].+;)password=([0-9A +-Za-z].+)/){ $myDSN=$1; $user=$2; $password=$3; chop($myDSN); chop($user); } $dsn ="dbi:Oracle:$myDSN"; $dbh = DBI->connect ($dsn, $user, $password, { PrintError =>1, RaiseError =>1 }) or die "Database connection not made: $DBI::errstr"; if($procName ne ''){ # This is the default call to stored proc. $procedure = "BEGIN "."$procName"."(?, ?); "."END;"; print $procedure, "\n"; print "After Connection\n"; $function = $dbh->prepare($procedure); $function->bind_param_inout(1, \$retCode, 100); $function->bind_param_inout(2, \$retMsg, 100); #These are the optional stored procs. if($procName eq "PAR_REPORTS_POS.SUN_PRC_AT_I_POSM1W001"){ $procedure = "BEGIN "."$procName"."(?, ?, ?, ?); "."END;"; $function = $dbh->prepare($procedure); $function->bind_param_inout(3,$finRepId,SQL_VARCHAR); $function->bind_param_inout(4,$showPendingPolicies,SQL_VARCHAR); } if($cycleDate ne ""){ $procedure = "BEGIN "."$procName"."(?, ?); "."END;"; $function = $dbh->prepare($procedure); $function->bind_param_inout(2, $cycleDate,SQL_VARCHAR); } if($procName eq "PAR_REPORTS_POS.SUN_PRC_AT_I_POSM1D007"){ } if($procName eq "PAR_REPORTS_POS.SUN_PRC_AT_I_POSM1M005"){ if($fromDate ne ""){ $procedure = "BEGIN "."$procName"."(?, ?, ?); "."END;"; $function = $dbh->prepare($procedure); $function->bind_param_inout(3,$fromDate,SQL_VARCHAR); } if($toDate ne ""){ $procedure = "BEGIN "."$procName"."(?, ?, ?); "."END;"; $function = $dbh->prepare($procedure); $function->bind_param_inout(3,$toDate,SQL_VARCHAR); } } print (scalar(localtime), " Proc Start", "\n"); $function->execute; print (scalar(localtime), " Proc End", "\n"); print ("ReturnCode : ".$retCode, "\n"); print ("ReturnMsg : ".$retMsg, "\n"); if($retCode ne 0){ print("Proc failed. Return code: ".$retCode); exit(-1); } $dbh->disconnect;
Please advise me if the default and optional prepare statements are correct and there should be an execute statement in every block or just a single execute statement. Thanks

Janitored by holli - added readmore-tag


In reply to DBI Execute problem by Nesh

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.