Send result of SQL procedure in a file with a script sh - sql-server

I have a SQL procedure in a file and I would like to call this procedure in a sh script then send the result in a file (no matter where is the file).
I do not know if I am doing well or not but here what I wrote :
#!/bin/ksh
#exit 0
VAR=$(sqlplus /NOLOG <<!
connect E760/E760#(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST=p595n11)(PORT=1521))(CONNECT_DATA = (SID = DBUVNSD5)))#PA_IND_DELAYS_BODY.sql;
!)
result=`sqlplus / <<SCRIPT| grep ^result | sed "s/^result: //"
set serveroutput on
declare
var_truc_result VARCHAR2(255);
begin
export_csv('$1','$2');
dbms_output.put_line( 'result: ' || var_truc_result);
end;
/
exit
SCRIPT`
sqlplus "system/ADMIN" <<!
exec export_csv($0,$1);
!
echo var_truc_result is: "$result"

After some search, I created and tested a small code snippet to get the printed text from dbms_output.
#!/bin/bash
result=$(sqlplus64 -S <username>/<password>#<IPADDRESS>/<SERVICENAME> << EOF
set serveroutput on;
begin
dbms_output.put_line('hello '||'$0');
end;
/
exit;
EOF
)
echo "result=$result"
You just need to process $result variable to grep your output.
I ran it under debian 8, I do not have ksh but used bash and my sqlplus bin name sqlplus64.
sqlplus -S argument explanation
-S Sets silent mode which suppresses the display of
the SQL*Plus banner, prompts, and echoing of
commands.
Moreover, here is another stackoverflow question and good answers to get select query results from sqlplus.
I hope this helps you to solve your problem.

Related

Retrieve and save SQLPLUS query result into a variable bash

I am trying to save the response of a query made through SQLPLUS and save it in a local variable, but when i execute the following code, i get the path as output instead of the value of the query, could u please help me? I don't know what am i doing wrong:
#!/bin/bash
SQLPLUS="<Path to sqlplus> -s user/passwd"
X=$SQLPLUS<<EOF_SQL_1
set heading off;
select table1 from table 2 where parameter ='Properties';
exit;
EOF_SQL_1
echo $X
The result of this script is " -s user/passwd" when it should be the esult of the query I made.
Please tell me what am I doing wrong :S
using heredoc and command substitution in the same command would not be recommended, the easier is to use a function
fun_sql() {
sqlplus user/passwd#tnsname <<EOF
...
EOF
}
X=$(fun_sql)

How to get the SQL Server script error in a batch File

I am calling an SQL Server script on a batch file, but I need get the error(when the script fail) in the batch file, what can I do?
This is the batch file:
sqlcmd -S HOST -U User -P password -i test.sql
echo %errorlevel%
and this is the Script File(Test.sql):
USE TrainingSitecore_Web
go
SELECT * FROM items
go
RAISERROR ('This is a test Error.',-- Message text.
16,-- Severity.
1 -- State.
);
Make sense?
It can be done using -r parameter:
Example:
sqlcmd -Q "select 1 as a; select 1/0 as b" -E -r1 1> NUL
-r[ 0 | 1] msgs to stderr
Redirects the error message output to the screen (stderr). If you do
not specify a parameter or if you specify 0, only error messages that
have a severity level of 11 or higher are redirected. If you specify
1, all error message output including PRINT is redirected. Has no
effect if you use -o. By default, messages are sent to stdout.
MSDN for more

Launch PL/SQL script from batch file (with arguments)

I'm strugling at passing arguments to a PL/SQL script from a Windows .bat file.
Here is the content of the batch file :
#echo off
set mName = "test"
exit | sqlplus <connection_string> #test.sql %mName%
pause
And here is the content of test.sql :
set verify off
set serveroutput on size unlimited
BEGIN
DBMS_OUTPUT.PUT_LINE('&&1');
END;
/
I would expect to see "test" appearing in the shell but instead, I get the following message :
Enter value for 1:
SP2-0546: User requested Interrupt or EOF detected.
Does anyone have a solution to this problem ?
Remove the spaces around your = sign. Here's my working batch file:
#echo off
set mName="test"
exit | sqlplus <connection_string> #test.sql %mName%
pause
Also note that you can use the -L option on sqlplus for batch jobs:
-L Attempts to log on just once, instead of reprompting on error.

using isql across multiple ksh scripts

I very new to ksh script but I have 2 ksh scripts each of them calling sybase stored procedure via isql. The issue I'm seeing is that when I execute the first script the stored procedure runs fine but when I execute the second it fails with error of (isql -b -S value -U value -P value: not found). Here are code snipets
Values for $SERVER, $DBO_USER and $DBO_PASSWORD are set earlier in the script.
test1.ksh:
ISQL_CMD="isql -b -S ${SERVER} -U ${DBO_USER} -P ${DBO_PASSWORD}"
VAR=`${ISQL_CMD} << EOF
set nocount on
go
set proc_return_status off
go
declare #var_id int
,#rtnval int
exec #rtnval = DB_NAME..MY_STORED_PROC_1
#parameter1 = ${VAR_IN}
,#parameter_output = #var_id output
go
EOF`
This executes fine and I get value in VAR variable
test2.ksh (VAR variable gets passed in from test1.ksh):
ISQL_CMD="isql -b -S ${DSQUERY} -U ${DBO_USER} -P ${DBO_PASSWORD}"
RETURN_VALUE=`${ISQL_CMD} << EOF
set nocount on
go
declare #rtnval int
exec #rtnval = DB_NAME..MY_STORED_PROC_2
#var_id = '${VAR}'
go
EOF`
I get the following error:
isql -b -S value -U value -P value: not found
These scripts can be run independent of each other so there is no guarantee that isql may have been called before test2.ksh and that is why I set the the ISQL_CMD variable in each script.
test1.ksh runs properly but test2.ksh does not, whether called from test1.ksh or run on it's own. Tried running the scripts in debug, but it didn't really provide any further information.
Ok I have figured this out after a full day of head scratching. test2.ksh is performing some file processing and setting the IFS value several lines prior to isql command. I had to reset or unset the IFS once I was done and isql command worked fine! the command to unset the IFS value is:
unset IFS
Thanks for those that were trying to help!!

Sybase stored proc called from isql on AIX: how to handle return code

I've got an AIX batch job that uses isql to execute a stored procedure in Sybase. The stored procedure is returning an error code under certain conditions. I would like for isql to pass that return code to the AIX script.
Can I capture the the stored proc's return code within isql, or do I have to write an output file of some kind and have my AIX script check that?
Here's what the isql command looks like. It's running inside a Korn shell script.
isql -D$database -S$server -U$userId -P$password << EOF
EXEC MY_STORED_PROC $AN_INPUT_PARAMETER
go
EOF
If I remember correctly, the $? is set to the command return value.
Add something like this after the EOF line:
if [[ $? != 0 ]]; then
print "stored procedure failed"
exit
fi
Is this what you wanted to achieve?
isql -D$database -S$server -U$userId -P$password << EOF | grep RETVAL | awk -F"=" '{print $2}' | read value
declare #retval int
EXEC #retval = MY_STORED_PROC $AN_INPUT_PARAMETER
SELECT "RETVAL=" + convert(varchar, #retval)
go
EOF
echo "Procedure returned: $value"
Add --retserverror in the ISQL command and then use $? as mentioned by Nikolai.

Resources