Calling sql file from shell script with parameters.

Hi,
I am calling a sql file script.sql from shell script and passing few parameters also as shown below:

 sqlplus -S  id/password @script.sql  $param1 $param2 

Now,In sql file I have to create a extract text file after querying oracle tables based on the parameters passed(param1,param2) as shown below:

 
SPOOL  a.txt
 
SELECT col1,col2,col3,........coln
FROM table
where col1 ='&1' and col2=`&2';
/
SPOOL OFF

This may contain thousand of rows. The extract file will contain data row by row and each column value will be separated with a tab space. I am using SPOOL to this but the extract file is giving me values of single row in multiple rows and the passed parameter values (param1 & param2) are also getting printed in the extract file at the top with old and new labels, which I don't want to be present in my extract file. I just want the data rows in my extract file separated by tab.
I also want some status code to be returned back to my shell script which will determine whether the sql file is successfully executed or not based on which error message will be sent via email.

Please help me with the sample examples to resolve this problem.

Thanks in advance.

Regards,
Anil

That's due to the default column lengths being insufficient for the column data. Since you want the output to be tab separated, you will have to append the columns using the tab character, which should eliminate this problem.

That's because of the default value ("ON") of the sqlplus variable "verify". Set this value to "OFF" like so -

set verify off

As mentioned earlier, you'll have to append all the columns using the tab character.

select col1||chr(9)||col2||chr(9)||... <and so on>

One easy way to do this is to add the "WHENEVER" sqlplus command at the beginning of your sqlplus session.

whenever sqlcode exit 1

Due to this, the sqlplus session will terminate in case of an error and the exit status ($?) will be set to 1. You can then check this value to take further action - sending an email, for instance.

See above.

HTH,
tyler_durden

Hi Tyler,
Thanks for your reply.It really helped.But now i have another problem.
I am trying to send mail from shell script based on the exit status ($?) of the sqlplus. The code I am using is below:

 
#!/bin/sh/
............................
............................
sqlplus -S id/password @script.sql $param1 $param2
 
echo "Sucess code is $?"  
 
if [$? = 1]
then
mail -s "Error Message" abc@xyz.com <aaa/bbb/data.txt
fi

The mail will be sent to �abc@xyz.com� and the content of the mail will be read from file aaa/bbb/data.txt with subject as �Error Message�. The above code is getting failed with the error message �Not found� with the line number of �if [$? = 1]� despite of printing the value of $? correctly.Can anyone tell me where I am going wrong?
One more thing in my extract file the data is getting populated from 2nd line.Can anyone tell me the reason for that? I want it from 1st line only.

Thanks in advance.

Regards,
Anil

"[" is a shell builtin test operator. And because it is an operator, it should be surrounded by spaces. Otherwise the shell gets confused (because the operand and the operator are indistinguishable).

tyler_durden

Hi Tyler,
That worked.Thanks a ton.Can you just tell me the reason for having data in extract file from 2nd row not from first?

Thanks,
Anil

Sorry, I don't think I understand your question.
You want me to tell you why your extract file has data from 2nd row and not from the first ??

Is your SQL statement similar to the following ?

SELECT col1||chr(9)||col2||chr(9)||col3
FROM table
where col1 ='&1' and col2=`&2';

I have no idea why Oracle would omit the first row.
Unless there's something else you want to add.

tyler_durden

Hi Tyler,
My extract file is fetching all the data from table but the problem is inspite of populating the rows from 1st line,it is getting inserted from 2nd row.Problem is that first line is blank.I just have to move whole data one row up.I hope it is clear now.

Thanks,
Anil

I'll have to see it to believe it.

(1) Open a sqlplus session, and execute your query in there. Copy/paste the sqlplus session (query + results) over here.

(2) And then copy/paste the spooled file (the one generated by your shell script, that contains the resultset) over here.

tyler_durden