using isql across multiple ksh scripts - sybase

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!!

Related

SQL where greater than in groovy script

The following groovy script does nothing:
def cmd = /sqlcmd -S 127.0.0.1\MSSQLSERVER -d LocalDevelop10DB -Q "DELETE FROM T_TimeRegistration WHERE TimeRegLineNr > 36"/
cmd.execute()
While this groovy script works perfectly:
def cmd = /sqlcmd -S 127.0.0.1\MSSQLSERVER -d LocalDevelop10DB -Q "DELETE FROM T_TimeRegistration WHERE TimeRegLineNr = 37"/
cmd.execute()
I want to use the (effects of) first script. It seems the '>' character is somehow not supported, I tried escaping it but no joy. What am I missing, can someone help?
Thanks
I don't know Groovy at all, but if you want to avoid the greater than symbol, you could use between:
DELETE FROM T_TimeRegistration
WHERE TimeRegLineNr between 37 and 2147483647
2147483647 is maximum int value
Do you have any error when your run the script.
Try to run the query (DELETE FROM T_TimeRegistration WHERE TimeRegLineNr > 36) from SSMS and see if it works, or see why it doesn't work.
Maybe you have some fk restrictions and one of rows is refered in other table.

Send result of SQL procedure in a file with a script sh

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.

Error While using SQLCMD in PDW

I'm using SQLCMD in PDW for extracting data into a flat file. The command line syntax is given below:
sqlcmd -S "10.20.30.40,19001" -d MyPDW_DB -U PDW_User -P Password1 -Q "SET QUOTED_IDENTIFIER ON; SELECT * FROM MyPDW_DB.dbo.SampleFact" -o "FactOut.txt" -s"|"
When I try to execute the batch file, I get the following error:
Msg 104409, Level 16, State 1, Server PdwTdsServer, Line 1
Setting QuotedIdentifier to 'OFF' is not supported.
I am assuming this is due to the fact that there is a "comma" in the server name (IP address,Port Number). I can use this command for extracting data from SQL tables. Any idea on how I can make this working for PDW?
Thanks in advance
I got this working partially.
sqlcmd -S "10.20.30.40,19001" -d MyPDW_DB -U PDW_User -P Password1 -I -Q "SELECT * FROM MyPDW_DB.dbo.SampleFact" -o "FactOut.txt" -s"|"
For setting the quoted_identifier OFF, the option to use is "-I". However, I'm still trying to find an alternative for "SET NOCOUNT ON" option which is not supported in PDW. If someone can help me with that, I'd greatly appreciate that.

Using variables in SQLCMD for Linux

I'm running the Microsoft SQLCMD tool for Linux (CTP 11.0.1720.0) on a Linux box (Red Hat Enterprise Server 5.3 tikanga) with Korn shell. The tool is properly configured, and works in all cases except when using scripting variables.
I have an SQL script, that looks like this.
SELECT COLUMN1 FROM TABLE WHERE COLUMN2 = '$(param1)';
And I'm running the sqlcmd command like this.
sqlcmd -S server -d database -U user -P pass -i input.sql -v param1="DUMMYVALUE"
When I execute the above command, I get the following error.
Sqlcmd: 'param1=DUMMYVALUE': Invalid argument. Enter '-?' for help.
Help lists the below syntax.
[-v var = "value"...]
Am I missing something here?
You don't need to pass variables to sqlcmd. It auto picks from your shell variables:
e.g.
export param1=DUMMYVALUE
sqlcmd -S $host -U $user -P $pwd -d $db -i input.sql
In the RTP version (11.0.1790.0), the -v switch does not appear in the list of parameters when executing sqlcmd -?. Apparently this option isn't supported under the Linux version of the tool.
As far as I can tell, importing parameter values from environment variables doesn't work either.
If you need a workaround, one way would be to concatenate one or more :setvar statements with the text file containing the commands you want to run into a new file, then execute the new file. Based on your example:
echo :setvar param1 DUMMYVALUE > param_input.sql
cat input.sql >> param_input.sql
sqlcmd -S server -d database -U user -P pass -i param_input.sql
You can export the variable in linux. After that you won't need to pass the variable in sqlcmd. However, I did notice you will need to change your sql script and remove the :setvar command if it doesn't have a default value.
export dbName=xyz
sqlcmd -Uusername -Sservername -Ppassword -i script.sql
:setvar dbName --remove this line
USE [$(dbName)]
GO
I think you're just not quoting the input variables correctly. I created this bash script...
#!/bin/bash
# Create a sql file with a parameterized test script
echo "
set nocount on
select k = '-db', v = '\$(db)' union all
select k = '-schema', v = '\$(schema)' union all
select '-', 'static'
go" > ./test.sql
# capture input variables
DB=$1
SCHEMA="${2:-dbo}"
# Exec sqlcmd
sqlcmd -S 'localhost\lemur' -E -i ./test.sql -v "db=${DB}" -v "schema=${SCHEMA}"
... and tested it like so:
$ ./test.sh master
k v
------- ------
-db master
-schema dbo
- static

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