Skip to Main Content
  • Questions
  • Passing parameter for SQL query using shell script

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Ashish.

Asked: April 24, 2001 - 2:45 pm UTC

Last updated: March 02, 2006 - 12:33 pm UTC

Version: Oracle 8.0.4

Viewed 10K+ times! This question is

You Asked

Hello Tom,
I am doing following thing,
sqlplus -s /nolog <<EOF
connect system/$pwd
@hdr
Column tablespace_name format a25 heading "TABLESPACE"
Column initial_extent format 99,999,999 heading "INITIAL"
Column next_extent format 99,999,999 heading "NEXT"
Column min_extents format 99,999,999 heading "MIN"
Column max_extents heading "MAX"
Column CONTENTS format a9 heading "CONTENTS"
TTitle left "***** Database: "dbname", Tablespace Information ( As of: "xdate" ) *****" skip 2
spool temp.txt
select
tablespace_name,
initial_extent,
next_extent,
min_extents,
decode(max_extents,2147483645,'UNLIMITED',max_extents) max_extents,
CONTENTS
from
dba_tablespaces
where tablespace_name=upper('&tbsp');
spool off
clear columns
EOF
But when I ran the script from shell it dosn't wait for the parameter and come out from the shell.
I want to do above query using shell script. Could you suggest me the solution?
Thanks,
Ashish Kher.

and Tom said...

Well, it looks like you are trying to do an "interactive" thing (&tbsp) in a non-interactive environment <<EOF.


Simply change &tbsp into $1 and pass the argument into the script. eg:




$ cat test.csh
#!/bin/csh

sqlplus -s /nolog <<EOF
connect /

Column tablespace_name format a25 heading "TABLESPACE"
Column initial_extent format 99,999,999 heading "INITIAL"
Column next_extent format 99,999,999 heading "NEXT"
Column min_extents format 99,999,999 heading "MIN"
Column max_extents heading "MAX"
Column CONTENTS format a9 heading "CONTENTS"
spool temp.txt
select
tablespace_name,
initial_extent,
next_extent,
min_extents,
decode(max_extents,2147483645,'UNLIMITED',max_extents) max_extents,
CONTENTS
from
dba_tablespaces
where tablespace_name=upper('$1');
spool off
clear columns
EOF


$ ./test.csh system
Connected.

TABLESPACE INITIAL NEXT MIN
------------------------- ----------- ----------- -----------
MAX CONTENTS
---------------------------------------- ---------
SYSTEM 16,384 16,384 1
505 PERMANENT





Rating

  (5 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thanks

Ashish Kher, April 25, 2001 - 8:57 am UTC

Hello,
I tried out with &$ but it didn't work. Actually I am working on menu driven monitoring tool using shell script. And I was stuck up for this problem.

Thank you very much for your immediate response.

Ashish Kher.

a related question

John, July 08, 2003 - 4:38 pm UTC

How to run non-interactive SQL*Plus in Windows invironment? I'm try to automate a process that calls a stored procedure. I created a .bat file like this but it's not working.

sqlplus jw/jw@mars << EOF
exec add_to_mytest;
exit;
EOF


Tom Kyte
July 08, 2003 - 6:16 pm UTC

echo exec add_to_mytest | sqlplus jw/jw@mars


should work, as would:

echo exec add_to_mytest > tmp.sql
echo exit >> tmp.sql
sqlplus jw/jw@mars @tmp
erase tmp.sql



can also...

Bricklen, July 08, 2003 - 7:06 pm UTC

add the -s option to the line, like:

sqlplus -s user/pass ...

to make it run in silent mode (doesn't display in the screen etc)

Thank you both, Tom and Bricklen!

John, July 08, 2003 - 7:39 pm UTC


need some more help on this

Vijay, March 02, 2006 - 8:11 am UTC

Hi Tom,
good day to you, as always your site has lot of information on whatever one can think about in Oracle, I have to work on a project in which we will be calling some procedure from shell scripts, I have never worked on calling procedure from shell scripts so just started searching your site and came on this posting.

It will be a great help if you can point me some document link that I can go through to equipt myself with the knowledge of all the nitty-gritty involved in interaction of Oracle from Shell scripts.

Kindest Regards,
Vijay Sehgal.

Tom Kyte
March 02, 2006 - 12:33 pm UTC

You would be asking for a shell scripting book of which I do not have a recommendation for.

For you see, there is no interaction between Oracle and shell scripts - there is interaction between shell scripts and executables (like sqlplus) and the interaction between shell and sqlplus is not any different than shell and "ls"