calling a sql file in my shell script

Hi,

I want to call a sql file in my shell script. see the below code:-

 if [[ "${MODE}"="FULL" ]]
 then
 (
       isql -U${S_USER} -S${S_SERV} -w100 -b -h0 <<ENDSQL | sed -e "s/Password://"
       ${S_PWD}
       set nocount on
       go
       use ${S_DB}
       go
       // need to call a file name XYZ.sql 
       go
       ENDSQL
 )

if the mode =full, then connect via using isql (command) with given paramters as (server_user, server_id, password).

xyz.sql file contains like 20sql commands in it. i am not sure how will i call that file in here. {i want to call the file because i am using that same file in different calls also, thats why not pasting sql commands here}

please help.. ??

Check for -i switch of isql

Also,

if [[ "${MODE}"="FULL" ]] 

should be

if [[ "${MODE}" = "FULL" ]]

as u said the mode is proper only. i checked it again.

if [[ "${MODE}" = "FULL" ]]

btw can you tell me what do you mean by isql -i ?

when i am compiling the code i am getting error

isql -U${S_USER} -S${S_SERV} -w100 -b -h0 <<ENDSQL | sed -e "s/Password://"

error - "<< unexpected" why its not taking the endsql ??? or throwing a error here

ending ENDSQL MUST be in the beginning of the line.

I understood, you wish to take queries from an input file instead writing.
that's why I suggested -i option.

from the sybase man page ( I assume you using sybase databse ?)

-i inputfile

    specifies the name of the operating system file to use for input to isql. The file must contain command terminators (�go� is the default).

        *

          Specifying the parameter as follows is equivalent to < inputfile:

          -i inputfile
        *

          If you use -i and do not specify your password on the command line, isql prompts you for it.
        *

          If you use < inputfile and do not specify your password on the command line, you must specify your password as the first line of the input file.

1 Like

thanks a ton for the answer and sorry for late reply.

Yes i am using sybase db. i understood the -i option and started using it also. about the endsql i am trying it out right now

 << FINSQL isql -U${S_USER} -S${S_SERV} -D${S_DB} -b -h0 -w1000 -i${MYDIR}/abc.sql | sed -e "s/Password://"

is this what ru suggesting. ?

---------- Post updated at 04:15 AM ---------- Previous update was at 04:11 AM ----------

i tried the upper code.

<< FINSQL isql -U${S_USER} -S${S_SERV} -D${S_DB} -b -h0 -w1000 -i${MYDIR}/abc.sql | sed -e "s/Password://"

but its still throwing me the same error.
ERROR - - " ./mine.ksh[6]: syntax error at line 13 : `<<' unmatched"

see the exact code below :-

MYDIR=/home/cadessad/bin

Retrieve_Date()
{
  << FINSQL isql -U${S_USER} -S${S_SERV} -D${S_DB} -b -h0 -w1000 -i${MYDIR}/abc.sql | sed -e "s/Password://"

i m just trying to configure this thing properly but unable to do it properly

No, I was talking about the ending label. ( the second one)

isql -U${S_USER} -S${S_SERV} -w100 -b -h0 <<ENDSQL | sed -e "s/Password://"
       ${S_PWD}
       set nocount on
       go
       use ${S_DB}
       go
       // need to call a file name XYZ.sql 
       go



ENDSQL ## this MUST be in the beginning of the line. no space or tabs before it

EDIT: Label name doesn't matters. It can be anything. SQL, sql,hi or even your name

1 Like

hey anchal thanks a lot. i tried putting finsql at the beginning of the line and its working properly.

i tried running again and i am getting anther error. (errors love me.. :stuck_out_tongue: ).

got any ideas to solve this error. see the below piece of code.

        isql -U${S_USER} -S${S_SERV} -D${S_DB} -b -h0 -i${MYDIR}/abc.sql <<FINSQL | sed -e "s/Password://"
     ${S_PWD}

when this code is executed .. below is the command for the execution of the script.

]./mine.ksh -S 3 -M FULL >>log.txt 

(mine.ksh is my shell script), it has 2 modes S and M. in the log.txt file the error i m facing is ..

 CT-LIBRARY error:   ^?}^]: user api layer: external error: A data length of 85 exceeds the maximum length allowed for password data. 

i tried using the isql command with -w1000 option also bt i was getting the same error. any heads up on this.?

Can you try this?

isql -U${S_USER} -S${S_SERV} -D${S_DB} -P${S_PWD} -b -h0 -i${MYDIR}/abc.sql << FINSQL

And remove the sed part now

i used the command as u said but i am still unable to connect to the database.

i am not sure if this is the code error or configuration error.

btw i am executing a sql query like this --

select distinct A.account_no into #sun

am i using the temp. variable #sun properly or there is somthing else i need to use with it.

what error you are getting now?
also, there must be some table name after "into" i guess.
#sun is a wrong interpretation.

yippe. (thanks a ton) . i figured out hw to call a file finally. bt hw do i interpret this data in the fields required by me is the next step. :o:o..

select account_no into tempdb..sun from ABC
go

i am selecting account_no into tempdb creating sun table and copying data from ABC. (this is working fine, i mean i am able to create the table):b:.

and the isql command i am running the one which u told me:-

Retrieve_Date()
{
if [[ "${MODE}" = "FULL" ]]
then
(
isql -U${S_USER} -S${S_SERV} -D${S_DB} -P${S_PWD} -b -h0 -i${MYDIR}/abc.sql << FINSQL
go
FINSQL
) > XYZ.txt
fi

I wanted to know how can i get the result of the sql query performed in abc.sql into a variable in my shell script. so that i can use those records. i am checking the database and i am seeing a temp. table being created but unable to get those records in my script. any ideas??

if you just want to output to a variable, you don't actually need here doc ( the labels method).

try something like:

Retrieve_Date()
{
if [[ "${MODE}" = "FULL" ]];then
 myvar=$(isql -U${S_USER} -S${S_SERV} -D${S_DB} -P${S_PWD} -b -h0 -i${MYDIR}/abc.sql)
fi
}

You must have "go" statement after each query in your ${MYDIR}/abc.sql file

hey, thanks a ton again.
actually i am pulling some data from abc.sql into temp.db. i am storing this in a table called XYZ in tempdb.

and i am using bcp tool so that i can get that data into my file.

i m using the below code :-

Retrieve_Date()
{
if [[ "${MODE}" = "FULL" ]]
then
(
isql -U${S_USER} -S${S_SERV} -D${S_DB} -P${S_PWD} -b -h0 -i${MYDIR}/abc.sql << FINSQL
go
FINSQL
) > sun.txt
fi
(
bcp tempdb..ASH in ${MYDIR}/takeit.txt -c -t"|" -U${S_USER} -S${S_SERV} -P${S_PWD} << FINBCP
FINBCP
) > ART.txt

I want the data fetched from abc.sql (queries) to be stored in takeit.txt (i actually want it in the file takeit.txt)
so that i can take ftp file and forward it some place else.

any ideas here??? :rolleyes::confused:

You don't even need variable if that is the case.

bcp is an stand alone utility which has command line switched for database details.
you don't need to interact with database directly. ( << thing ).

Retrieve_Date()
{
if [[ "${MODE}" = "FULL" ]];then
 isql -U${S_USER} -S${S_SERV} -D${S_DB} -P${S_PWD} -b -h0 -i${MYDIR}/abc.sql >/dev/null 2>&1
fi
}
## call the function
Retrieve_Date 
## process bcp
bcp tempdb..ASH out ${MYDIR}/takeit.txt -c -t"|" -b 100000 -S${S_SERV} -U${S_USER} -P${S_PWD}

in your "abc.sql", you must have something like this..

select blah blah into tempdb..ASH from blah
go
1 Like

thanks a lot.. again.

i think u wud have got bored of me thanking again and again. :stuck_out_tongue: :cool:

btw all this exercise of me learning gone to waste. those idiots want something else nw... GRRRR

bt i m thankful to them in a hideous way becuase i learnt a lot :smiley:

i will tried out the bcp command its not working currently. it gives the below error any clues??

Starting copy...
The BCP hostfile '/home/cadessad/bin/takeit.txt' contains only 0 rows. It was impossible to read the requested number of rows.
bcp copy in failed

well, the clue is in the message itself. :slight_smile:

Did you change the bcp syntax from in to out as I mentioned?

grrrrr me noob.
i was using out earlier when i used the command first time. bt then a friend said no it has to be in..
i will change it right now.. and check

---------- Post updated at 09:33 AM ---------- Previous update was at 09:28 AM ----------

see the below output of bcp.. it works ... yeeyeyeyeyeyeyeyyeyee :rolleyes::rolleyes::rolleyes::rolleyes::rolleyes::p:p:p:p

4||0|4|0|8|1|2|1|1|Aug  1 2000 12:00AM|Sep 19 2000 12:00AM|Oct 15 2000 12:00AM|22922|0|70|0||RESIDENCIAL CEUTA