Run stored procedure from shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run stored procedure from shell script
# 22  
Old 01-05-2012
yup, same as u suggested.

---------- Post updated at 08:28 AM ---------- Previous update was at 08:25 AM ----------
Code:
#!/bin/bash
 
        OUTPUT=$(isql -Usa -Ppassword -DNP_DB << EOF
        exec testing
        exit;
        EOF)
        echo output $OUTPUT
        echo last command status : $?
        if [ "$OUTPUT" != 0 ]
        then
            echo "error return from SP"
            exit
        else
            echo "SP return 0"
        fi

where stored procedure "testing" is returning 1 from it as below -
Code:
create proc testing
as
return 1

and I am getting output as -
Code:
[tecnomen]##/export/home/xyz> ./callgSPFromScript_new.sh
output
last command status : 0
error return from SP
[tecnomen]##/export/home/xyz>

---------- Post updated 01-05-12 at 01:49 AM ---------- Previous update was 01-04-12 at 08:28 AM ----------

Quote:
Originally Posted by PriyaSri
yup, same as u suggested.

---------- Post updated at 08:28 AM ---------- Previous update was at 08:25 AM ----------
Code:
#!/bin/bash
 
        OUTPUT=$(isql -Usa -Ppassword -DNP_DB << EOF
        exec testing
        exit;
        EOF)
        echo output $OUTPUT
        echo last command status : $?
        if [ "$OUTPUT" != 0 ]
        then
            echo "error return from SP"
            exit
        else
            echo "SP return 0"
        fi

where stored procedure "testing" is returning 1 from it as below -
Code:
create proc testing
as
return 1

and I am getting output as -
Code:
[tecnomen]##/export/home/xyz> ./callgSPFromScript_new.sh
output
last command status : 0
error return from SP
[tecnomen]##/export/home/xyz>


Hi all,

I am now abe to get the return value of SP using SELECT command. However I am not clear how to get this value in variable declared in script for my further processing. Can any one help me on this please. The code snippet is given below -
Code:
#!/bin/bash
`isql -Usa -Ppassword -DNP_DB << EOF
declare @returnValue int
exec @returnValue = testing
select ReturnValue=@returnValue
go
EOF`
echo $ReturnValue
echo last command status : $?
if [ "$OUTPUT" != 0 ]
then
echo "error return from SP"
exit
else
echo "SP return 0"
fi


Last edited by PriyaSri; 01-05-2012 at 02:50 AM.. Reason: Code tags
# 23  
Old 01-05-2012
Can u just modify the procedure as below and try once,

Code:
create procedure testing 
returning integer; 
return 1; 
end;

# 24  
Old 01-05-2012
Hi Siva,

This proc gives below error when I try to create -

Could not execute statement.
Incorrect syntax near 'returning'
Sybase error code=102, SQLState="42000"

I tried this previously also as suggested by you Smilie
# 25  
Old 01-05-2012
Fine, its seems that you are using Sybase DB then this would be the right syntax to create procedure (as already done by u)

Code:
create proc testing
as
return 1

As suggested by you, using SELECT you can able to store the return value, could you please print that value inside the SQL block and try to redirect it to one flat file. Based on the value we can manipulate using sed or awk command to get the exact return value.

Try like this,

Code:
#!/bin/bash
`isql -Usa -Ppassword -DNP_DB > file_return_value << EOF
declare @returnValue int
exec @returnValue = testing
select ReturnValue=@returnValue
print @returnValue
go
EOF`
echo $ReturnValue
echo last command status : $?
if [ "$OUTPUT" != 0 ]
then
echo "error return from SP"
exit
else
echo "SP return 0"
fi

Also provide me the content of that file (file_return_value)
# 26  
Old 01-05-2012
Try this ..

I don't have expertise in Sybase , I am giving sample programme in Oracle to capture the error using the shell script hope this will help you.

Sample procedure : You have to make sure the created procedure should raise an error whenever it has encountered any oracle or business rule error.
Code:
CREATE OR REPLACE PROCEDURE test123(p_in NUMBER) IS
BEGIN

  IF p_in = 1 THEN
    raise_application_error(-20000, SQLERRM);
  END IF;

EXCEPTION
  WHEN OTHERS THEN
    raise_application_error(-20000, SQLERRM);
END;

Shell script to capture the error occurence.
Code:
. /var/opt/oracle/oravars9208
sqlplus -s "username/password@DBNAME" <<EOF
WHENEVER SQLERROR EXIT SQL.SQLCODE
--call the procedure
execute test123(p_in => 1);
exit
EOF
ret_cd=$?
if [ $ret_cd -eq  0 ]
then
echo "Successfully Executed"
else
echo "Error Occurred"
fi

Cheers,
Palani

Last edited by palanisvr; 01-05-2012 at 06:25 AM..
# 27  
Old 01-05-2012
Quote:
Originally Posted by Rksiva
Fine, its seems that you are using Sybase DB then this would be the right syntax to create procedure (as already done by u)

Code:
create proc testing
as
return 1

As suggested by you, using SELECT you can able to store the return value, could you please print that value inside the SQL block and try to redirect it to one flat file. Based on the value we can manipulate using sed or awk command to get the exact return value.

Try like this,

Code:
#!/bin/bash
`isql -Usa -Ppassword -DNP_DB > file_return_value << EOF
declare @returnValue int
exec @returnValue = testing
select ReturnValue=@returnValue
print @returnValue
go
EOF`
echo $ReturnValue
echo last command status : $?
if [ "$OUTPUT" != 0 ]
then
echo "error return from SP"
exit
else
echo "SP return 0"
fi

Also provide me the content of that file (file_return_value)
Hi Siva,

Please find the content of the file below -
Code:
[ctp-1-zone564:tecnomen] /export/home/tecnomen/> cat file_return_value
(return status = 1)
 ReturnValue
 -----------
           1
(1 row affected)
Msg 2737, Level 16, State 1:
Server 'SYBASE', Line 4:
Message passed to PRINT must be of type CHAR or VARCHAR.
[ctp-1-zone564:tecnomen] /export/home/tecnomen/>

Thanks.
# 28  
Old 01-05-2012
Fine, take that return status from that file and store it into variable which u r trying to do manupulations.

Code:
variablename = `grep "return status" file_return_value | cut -d'=' -f2 | sed 's/\)//g'`

This User Gave Thanks to Rksiva For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get OUT parameter of a stored procedure in shell script?

I am invoking a SQL script from shell script. This SQL script will invoke a stored procedure(which has the OUT parameter). I want to have the OUT parameter in the shell script as a variable. Is this possible? (6 Replies)
Discussion started by: vel4ever
6 Replies

2. Shell Programming and Scripting

How to call stored procedure with CLOB out parameter from shell script?

I have written a stored procedure in oracle database, which is having a CLOB OUT parameter. How can i call this stored procedure from shell script and get the CLOB object in shell script variable? (0 Replies)
Discussion started by: vel4ever
0 Replies

3. Shell Programming and Scripting

How to execute the stored procedure from shell script

How to execute the stored procedure from shell script and is there any possibility to print the dbms output in a log file. (2 Replies)
Discussion started by: dineshmurs
2 Replies

4. Shell Programming and Scripting

Passing a value to stored procedure from unix shell script

Hi Dudes :) I want a unix shell script to pass value to SQL stored procedure. Below is the procedure declare res varchar2(10); begin odm_load_check('PRE_SANITY',res); dbms_output.put_line(res); end; select * from error_log; truncate table error_log; select * from test; (1 Reply)
Discussion started by: shirdi
1 Replies

5. Shell Programming and Scripting

how to store the return values of stored procedure in unix shell script.

hi i am calling a oracle stored procedure(in the database) from unix shell scripting (a.sh). the called stored procedure returns some values through OUT variables i want to assign the return values of stored procedure in to unix shell script variable. can you provide me the code. ... (1 Reply)
Discussion started by: barani75
1 Replies

6. Shell Programming and Scripting

Invoking Oracle stored procedure in unix shell script

Here's a shell script snippet..... cd $ORACLE_HOME/bin Retval=`sqlplus -s <<eof $TPDB_USER/april@$TPD_DBCONN whenever SQLERROR exit 2 rollback whenever OSERROR exit 3 rollback set serveroutput on set pages 999 var status_desc char(200) var status_code... (1 Reply)
Discussion started by: hidnana
1 Replies

7. Shell Programming and Scripting

passing parameter 4m shell script to a DB stored procedure

hi all please tell me how to pass parameters 4m shell script to a DataBase stored procedure. To be specific i have sybase DB. i mean i want the syntax of the command.. how to connect to DB, pass user id and password, pass the required parameter to SP.. .. need ur help frnds.. hema (0 Replies)
Discussion started by: hema2026
0 Replies

8. Shell Programming and Scripting

Calling an Oracle Stored Procedure from Unix shell script

hai, can anybody say how to call or to execute an oracle stored procedure in oracle from unix... thanks in advance.... for ur reply.... by, leo (2 Replies)
Discussion started by: Leojhose
2 Replies

9. Shell Programming and Scripting

Calling stored procedure from shell script

HI, I have a similar problem to thread 18264, only I couldn't get it to work. https://www.unix.com/showthread.php?t=18264 I have a stored procedure which is called by a shell script program. When I run the stored procedure alone or through the shell script, it works fine with output text... (3 Replies)
Discussion started by: dorisw
3 Replies

10. Shell Programming and Scripting

calling stored procedure from shell script.

Hi All, This is a very starnge problem I am having. I have a shell script that calls a stored procedure. Here's my code in shell script: sqlplus "userid/pwd" @file.sql and file.sql has the following statement: exec my_storedProc; Now, when I execute my shell script, nothing... (2 Replies)
Discussion started by: priyamurthy2005
2 Replies
Login or Register to Ask a Question