472,333 Members | 1,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,333 software developers and data experts.

calling a stored procedure from a unix script

Hi,

I don't have any experiance of UNIX and oracle.
but i have got a short assignment for writing a unix script that will call a stored procedure. this script has to run this procedure after every two mintes.

I have a few questions before actually start writing the script:

1. Do i have to have unix and oracle on same server.
2. what libraries i need to in unix, if any

I tried sqlplus user/password@databasename which i got in this forum only but it's saying ksh: sqlplus: not found

please help me out. Thanks in advance.
Oct 12 '07 #1
26 22150
debasisdas
8,127 Expert 4TB
Please find the details here.
Oct 12 '07 #2
amitpatel66
2,367 Expert 2GB
Hi,

I don't have any experiance of UNIX and oracle.
but i have got a short assignment for writing a unix script that will call a stored procedure. this script has to run this procedure after every two mintes.

I have a few questions before actually start writing the script:

1. Do i have to have unix and oracle on same server.
2. what libraries i need to in unix, if any

I tried sqlplus user/password@databasename which i got in this forum only but it's saying ksh: sqlplus: not found

please help me out. Thanks in advance.
Set the environment before calling sqlplus command.
Environment can be set using ". newfin" command.

Eg:

$ . newfin <instance name>
$ sqlplus user/pwd@dbname
Oct 12 '07 #3
Hi Amit,

Thanks for your response.

Can you please answer the two doubts which i have asked?
Oct 12 '07 #4
amitpatel66
2,367 Expert 2GB
Hi Amit,

Thanks for your response.

Can you please answer the two doubts which i have asked?
Seema,

Well I dont understand your question clearly?
What you mean by having UNIX and oracle in same server?

Basically its a Unix Box (unix OS) in which Oracle will be installed.
Oct 12 '07 #5
Seema,

Well I dont understand your question clearly?
What you mean by having UNIX and oracle in same server?

Basically its a Unix Box (unix OS) in which Oracle will be installed.
well - In my case, I have a unix box and then the database is lying on some other database server (different phisical machine)
so can't we connect to that database from this box like we connect to any remote database through java like using jdbc driver
Oct 12 '07 #6
amitpatel66
2,367 Expert 2GB
well - In my case, I have a unix box and then the database is lying on some other database server (different phisical machine)
so can't we connect to that database from this box like we connect to any remote database through java like using jdbc driver
Yes you can connect to the database located in another machine.
Get the server name,port and other connection related details.
Specify the TNS ENTRY and you can connect to that database by using tool like PUTTY or any other.
Oct 12 '07 #7
Yes you can connect to the database located in another machine.
Get the server name,port and other connection related details.
Specify the TNS ENTRY and you can connect to that database by using tool like PUTTY or any other.
Ok, yeah i got a Unix server now where oracle is also installed.
Now i searched for some sample script on net and got the one below for me:

#!/bin/sh
sqlplus UID/PWD@Hostname
exec my_proc
whenever SQLError exit faliure

but it doesn't work. do you see any issues with this.
Oct 24 '07 #8
amitpatel66
2,367 Expert 2GB
Ok, yeah i got a Unix server now where oracle is also installed.
Now i searched for some sample script on net and got the one below for me:

#!/bin/sh
sqlplus UID/PWD@Hostname
exec my_proc
whenever SQLError exit faliure

but it doesn't work. do you see any issues with this.
Do you have the HOSTNAME specified in your TNS Entry file?
Do you have the Proper UID and PWD?

Can you POST the exact error message that you are facing?
Oct 25 '07 #9
Do you have the HOSTNAME specified in your TNS Entry file?
Do you have the Proper UID and PWD?

Can you POST the exact error message that you are facing?
hi,
Thanks for all your responses.

Ok - it's working now - the user id - i was given by the DBA was not having excecute rights for the stored procedures.

need your help on - how do i know - that my stored procedure has run successfully or it has been failed.
there is some oncept of return codes - right? please help me on that.
Oct 29 '07 #10
amitpatel66
2,367 Expert 2GB
hi,
Thanks for all your responses.

Ok - it's working now - the user id - i was given by the DBA was not having excecute rights for the stored procedures.

need your help on - how do i know - that my stored procedure has run successfully or it has been failed.
there is some oncept of return codes - right? please help me on that.
Yes, you can either have a status flag as OUT parameter and return 0 or 1 based on execution of your procedure.

If u r performing any DML operations, then you can COMMIT within a procedure and do a manual check if the DML operation was carried out successfully on the tables!!
Oct 29 '07 #11
Yes, you can either have a status flag as OUT parameter and return 0 or 1 based on execution of your procedure.

If u r performing any DML operations, then you can COMMIT within a procedure and do a manual check if the DML operation was carried out successfully on the tables!!
Thanks Amit
for such a quick response.

the thing is that i am very new to Unix - this is my first assignment.
I was searching for a sample script on net - to capture return code and also if i can get the exact return code so that i can log that in a log file with the error message and error code.

like this time i am getting the following error:

DBD::Oracle::st execute failed: ORA-01403: no data found

also could you please tell what is the code for successfull execution.
Oct 29 '07 #12
amitpatel66
2,367 Expert 2GB
Thanks Amit
for such a quick response.

the thing is that i am very new to Unix - this is my first assignment.
I was searching for a sample script on net - to capture return code and also if i can get the exact return code so that i can log that in a log file with the error message and error code.

like this time i am getting the following error:

DBD::Oracle::st execute failed: ORA-01403: no data found

also could you please tell what is the code for successfull execution.
In case if you want tot capture the Error message and Error code, you can make use of SQLCODE and SQLERRM keywords.

SQLCODE - gives you error code
SQLERRM - gives you error message.

Include the EXCEPTION block in your procedure, and check for any exception, if any exception occurs, insert the data in toe err_log table.

Eg:

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE PROCEDURE xyz IS
  2. BEGIN
  3. <your code goes here>;
  4. EXCEPTION
  5. WHEN NO DATA FOUND THEN
  6. INSERT INTO err_log VALUES(SQLCODE,SQLERRM);
  7. END;
  8.  
In case of successful execution of procedure, SQL CODE will be 0
and SQLERRM will be "ORA-0000: normal, successful completion"
Oct 29 '07 #13
In case if you want tot capture the Error message and Error code, you can make use of SQLCODE and SQLERRM keywords.

SQLCODE - gives you error code
SQLERRM - gives you error message.

Include the EXCEPTION block in your procedure, and check for any exception, if any exception occurs, insert the data in toe err_log table.

Eg:

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE PROCEDURE xyz IS
  2. BEGIN
  3. <your code goes here>;
  4. EXCEPTION
  5. WHEN NO DATA FOUND THEN
  6. INSERT INTO err_log VALUES(SQLCODE,SQLERRM);
  7. END;
  8.  
In case of successful execution of procedure, SQL CODE will be 0
and SQLERRM will be "ORA-0000: normal, successful completion"
I cann't touch the stored procedure. I was looking for some solution in unix itself.

If i can get the error code and message in shell script. Like now when i run my shell script - at unix prompt i am getting that error message. If i can capture that message in script itself and then i can write that error message into a log file after attaching some more information to it.

I hope you are getting me.
Oct 30 '07 #14
amitpatel66
2,367 Expert 2GB
I cann't touch the stored procedure. I was looking for some solution in unix itself.

If i can get the error code and message in shell script. Like now when i run my shell script - at unix prompt i am getting that error message. If i can capture that message in script itself and then i can write that error message into a log file after attaching some more information to it.

I hope you are getting me.
Check out the below code would help:

Expand|Select|Wrap|Line Numbers
  1. sqlplus -s << END_OF_SQL
  2. UID/pwd
  3. VAR v_out_err_msg   NUMBER
  4. WHENEVER SQLERROR EXIT SQL.SQLCODE
  5. WHENEVER OSERROR  EXIT FAILURE
  6. SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF ECHO OFF SERVEROUTPUT ON SIZE 100000
  7.    EXECUTE <your procedure name>(:v_out_err_msg);
  8. EXIT :v_out_err_msg;
  9. END_OF_SQL
  10.  
  11. STATUS=$?
  12.  
  13. if [ $STATUS -ne 0 ]
  14. then
  15.    echo "Extract encountered errors during the process"   >>$LOG_FILE
  16.    exit $FAILURE
  17. else
  18.    echo "The extract file has been generated." 
  19.    echo "\nExtract completed successfully.\n" 
  20. fi
  21.  

You need to add one parameter atleast to check the execution status of your procedure as I have in the above code (ie v_out_err_msg)
Oct 30 '07 #15
ok,

because of client recomendation i have to use perl now.

in perl i am getting a return value of 1 in rc:
i guess 1 is for faliure but it's not giving any error also - any thoughts?

eval { $sql_cmd = "BEGIN\n$stored_proc_mauisecond( p_num_rec => :p_num_rec, p_upper_bound => :p_upper_bound)\;END\;";
$sth = $dbh->prepare( $sql_cmd );
$sth->bind_param(":p_num_rec",50);
$sth->bind_param(":p_upper_bound","10-10-2007");
my $rc=$sth->execute(); print LOGF scalar localtime(), ": Execution of stored procedure with return code: $rc\n";

};
if( $@ ) {
print LOGF scalar localtime(), ": Execution of stored procedure failed: $DBI::errstr\n";
$dbh->rollback;
# exit 0;
}
Oct 31 '07 #16
amitpatel66
2,367 Expert 2GB
ok,

because of client recomendation i have to use perl now.

in perl i am getting a return value of 1 in rc:
i guess 1 is for faliure but it's not giving any error also - any thoughts?

eval { $sql_cmd = "BEGIN\n$stored_proc_mauisecond( p_num_rec => :p_num_rec, p_upper_bound => :p_upper_bound)\;END\;";
$sth = $dbh->prepare( $sql_cmd );
$sth->bind_param(":p_num_rec",50);
$sth->bind_param(":p_upper_bound","10-10-2007");
my $rc=$sth->execute(); print LOGF scalar localtime(), ": Execution of stored procedure with return code: $rc\n";

};
if( $@ ) {
print LOGF scalar localtime(), ": Execution of stored procedure failed: $DBI::errstr\n";
$dbh->rollback;
# exit 0;
}
You guess is wrong.
1 is for success and 0 is for failure in perl!!
Your code is working fine and it returns perfect status ie 1 which means success
Nov 1 '07 #17
Oh - is it?

Thanku so much Amit.
You have been a great help to me :-)
Nov 1 '07 #18
amitpatel66
2,367 Expert 2GB
Oh - is it?

Thanku so much Amit.
You have been a great help to me :-)
You are Welcome :)

Amit
Nov 1 '07 #19
You are Welcome :)

Amit
Hi Amit,

I have all the database details(user name and password also) in the perl script - hard coded.

what should be the best way to hide this. I guess i should have that in a seperate file and use it in main perl script.

Can you please tell me what is the way in perl to do this
Nov 6 '07 #20
amitpatel66
2,367 Expert 2GB
Hi Amit,

I have all the database details(user name and password also) in the perl script - hard coded.

what should be the best way to hide this. I guess i should have that in a seperate file and use it in main perl script.

Can you please tell me what is the way in perl to do this
Yes, In case ig you are going to use the same database login UID,connection string, then you ca nretrive those by storing them in the file.

Is your application going to accept User/Password/Connection string from the users as an input values?
Nov 6 '07 #21
Yes, In case ig you are going to use the same database login UID,connection string, then you ca nretrive those by storing them in the file.

Is your application going to accept User/Password/Connection string from the users as an input values?
yes i was hoping to have the details in a configuration kind of file - like we have properties file in java.
so that when my script goes in production env - i just need to edit user name, pwd and db details in just this config file.

i searched on net - guess there rea a no of ways like 'require' , use .....

but i couldn't write the working code.

can you please gimme an example - what format should be the config file and then how can i extract values based on key
Nov 6 '07 #22
amitpatel66
2,367 Expert 2GB
yes i was hoping to have the details in a configuration kind of file - like we have properties file in java.
so that when my script goes in production env - i just need to edit user name, pwd and db details in just this config file.

i searched on net - guess there rea a no of ways like 'require' , use .....

but i couldn't write the working code.

can you please gimme an example - what format should be the config file and then how can i extract values based on key
The parameters file can be of any format might not be a problem. Let it be a txt file with User,password,connection string terminated with ":" (Colon)

Now from perl, open this file in read mode,extract data before every colon (taking a substr in general). Place this configuration parameter file in the same path/directory where your perl file resides.

I hope this helps!!
Nov 6 '07 #23
The parameters file can be of any format might not be a problem. Let it be a txt file with User,password,connection string terminated with ":" (Colon)

Now from perl, open this file in read mode,extract data before every colon (taking a substr in general). Place this configuration parameter file in the same path/directory where your perl file resides.

I hope this helps!!
Hi Amit,

In perl, I am using DBI to call stored procedure. May you tell me a way so that i can write dbms_output.put_line statement to a log file through perl or to the terminal.

like in Unix we can say set serveroutput on, what we can do in perl for that
Nov 15 '07 #24
amitpatel66
2,367 Expert 2GB
Hi Amit,

In perl, I am using DBI to call stored procedure. May you tell me a way so that i can write dbms_output.put_line statement to a log file through perl or to the terminal.

like in Unix we can say set serveroutput on, what we can do in perl for that
You can try below code in order to get the functionality of SERVEROUTPUT in perl:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #enable output
  3.   $dbh->func( 1000000, 'dbms_output_enable' );
  4. #execute proc $sp
  5.   my $sth=$dbh->prepare($sp);
  6.   $sth->execute();
  7. #retrieve output
  8.   my @text = $dbh->func( 'dbms_output_get' );
  9.  
  10.  
and @text contains what would have been printed in your SQLPLUS session with "SET SERVEROUTPUT ON"
Nov 15 '07 #25
You can try below code in order to get the functionality of SERVEROUTPUT in perl:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #enable output
  3.   $dbh->func( 1000000, 'dbms_output_enable' );
  4. #execute proc $sp
  5.   my $sth=$dbh->prepare($sp);
  6.   $sth->execute();
  7. #retrieve output
  8.   my @text = $dbh->func( 'dbms_output_get' );
  9.  
  10.  
and @text contains what would have been printed in your SQLPLUS session with "SET SERVEROUTPUT ON"
oh great
Thanks Amit

I shall let u know - how does that work for me :-)
Nov 15 '07 #26
amitpatel66
2,367 Expert 2GB
oh great
Thanks Amit

I shall let u know - how does that work for me :-)
You are welcome :)

We are glad that the solution helped you!!

Regards,
Amit
Nov 16 '07 #27

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: dw | last post by:
Hello all, I'm having a dickens of a time calling a stored procedure on a connection. Every time I do, it generates an error "Arguments are of the...
0
by: HumanJHawkins | last post by:
"HumanJHawkins" <JHawkins@HumanitiesSoftware.Com> wrote in message news:i9nac.8030$Dv2.2242@newsread2.news.pas.earthlink.net... > (The SQL Group...
0
by: JN | last post by:
Hello, I'm having problem calling stored procedures from Visual FoxPro database. I got the following exception error: ...
6
by: Scott McNair | last post by:
Hi all, I'm having problems calling a stored procedure from within my code. If I execute the SP manually from a Query Analyzer window it runs...
4
by: Jack | last post by:
Hi, I am trying to run an example code from a book. However I am getting the following error message: Number: -2147217900 Description: Syntax...
14
by: krishna1412 | last post by:
Currently i am working in a project of report generation in MS ACCESS. The tables are in sql server 2000. I have to write stored proc in ms...
4
by: eighthman11 | last post by:
I'm calling a stored procedure on a sql server from an access application. I just need the stored procedure to run I do not need any data returned...
2
by: =?Utf-8?B?YW5vb3A=?= | last post by:
Hello, I have a stored procedure named as usp_CheckLogin with two parameters as @usID, @Password also values of these parameters are to be...
3
by: yinzara | last post by:
I have the following trigger that calls a DB2 stored procedure: DROP TRIGGER GGWU.TRI_A_MULTI_PROP@ CREATE TRIGGER GGWU.TRI_A_MULTI_PROP AFTER ...
6
Soniad
by: Soniad | last post by:
Hello, I am excecuting a stored procedure in my ASP page , it has one out parameter (@confirm) . after executing the procedure i want to retreive...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.