CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to execute sql script from Perl script?

 



digioleg54
User

May 11, 2017, 10:04 AM

Post #1 of 5 (1623 views)
How to execute sql script from Perl script? Can't Post

I have an sql script in my Perl program

Code
my ($monitor_key,$out_dir) = @_; 
if (defined $monitor_key)
{
my $sql_script = $out_dir."/".$monitor_key.".process";
my $sql_log = $out_dir."/".$monitor_key.".process.log";
my $sql_error = $out_dir."/".$monitor_key.".process.err";
eval {
open (FILE, ">>$sql_script") or die "Cannot open $sql_script\n";

print FILE "WHENEVER OSERROR EXIT SQL.OSCODE";
print FILE "WHENEVER SQLERROR EXIT SQL.SQLCODE";
print FILE "set HEADING OFF\n";
print FILE "set FEEDBACK OFF\n";
print FILE "set PAGESIZE 0\n";
print FILE "set TERMOUT ON\n";
print FILE "set VERIFY OFF\n";
print FILE " select NVL(A.status, 'N/A')
from dte_monitor A, dte_parameter C
where C.parameter_name = 'MONITOR_DATE'
and TRUNC(C.parameter_date) = TRUNC(A.proc_date)
and A.KEY = upper($monitor_key)\n";
print FILE "quit\n";
close FILE;
};


Is it correct if I run it from Perl script as


Code
 
exec my $res = $sql_script ;

if($@) {
print STDERR "Errror......$!"
}
else{
if {
( $result = something)
return something;
}
else{
return something else;
}
}


Thanks


(This post was edited by digioleg54 on May 11, 2017, 10:13 AM)


Laurent_R
Veteran / Moderator

May 11, 2017, 11:28 PM

Post #2 of 5 (1613 views)
Re: [digioleg54] How to execute sql script from Perl script? [In reply to] Can't Post

No, that's most probably not what you want. The exec statement will launch another process and exit immediately. So your script will not be able to retrieve data nor even know if the sql script is successful.

Please check documentation for exec, system, `` (backticks) and qx//.


Laurent_R
Veteran / Moderator

May 11, 2017, 11:36 PM

Post #3 of 5 (1612 views)
Re: [digioleg54] How to execute sql script from Perl script? [In reply to] Can't Post

I should add that the $sql_script your building seems to lack the DQ connection boiler plate code, but may be you've included it before, since you're opening the file in append mode.

As a more general comment, it might be better to use the DBI module along the the proper driver for your database.


digioleg54
User

May 12, 2017, 10:10 AM

Post #4 of 5 (1601 views)
Re: [Laurent_R] How to execute sql script from Perl script? [In reply to] Can't Post

Yes, l will do it, but the most important for me how to check the result. In my case it always shows me an error


Laurent_R
Veteran / Moderator

May 12, 2017, 2:48 PM

Post #5 of 5 (1594 views)
Re: [digioleg54] How to execute sql script from Perl script? [In reply to] Can't Post

Please show the error message. Is it a compile time error (invalid syntax) or a run time error (an exeption upon execution)?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives