Skip to Main Content
  • Questions
  • Passing Shell Variables into SQL Block

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Vinnie.

Asked: June 18, 2004 - 2:03 pm UTC

Last updated: June 21, 2004 - 1:14 pm UTC

Version: 9.2.1

Viewed 1000+ times

You Asked

Tom,
I am trying to pass a shell variable into a SQL Block, lets call it $MYVAR

I have a table that is as follows:

MY_TEST
=======
start_time date
stop_time date
timeline number
owner varchar2(25)

I also have various tables in the schema that have TIMELINE as a column.

I would like to pass in a $MYVAR into a block, query the TIMELINE table for all unique TIMELINEs where owner = $MYVAR, then
delete rows in all tables that have a column named timeline & where timeline = $MYVAR.

Hope this is enough.





and Tom said...

depends on your shell (this is more of a "how do I program shell" than a "question about oracle")

In general, something similar to the following:


[tkyte@tkyte-pc tkyte]$ cat test.sh
#!/bin/bash

sqlplus / <<EOF

define H=$HOME
select '&H' from dual;
exit

EOF

[tkyte@tkyte-pc tkyte]$ ./test.sh

SQL*Plus: Release 9.2.0.5.0 - Production on Fri Jun 18 13:55:37 2004

Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.


Connected to:
Oracle9i Enterprise Edition Release 9.2.0.5.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.5.0 - Production

ops$tkyte@ORA9IR2> ops$tkyte@ORA9IR2> ops$tkyte@ORA9IR2> old 1: select '&H' from dual
new 1: select '/home/tkyte' from dual

'/HOME/TKYT
-----------
/home/tkyte

ops$tkyte@ORA9IR2> Disconnected from Oracle9i Enterprise Edition Release 9.2.0.5.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.5.0 - Production


will do it.

Rating

  (2 ratings)

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

Comments

For shame - no binds?

Ian Bird, June 21, 2004 - 7:38 am UTC

Tom

Would it not be better if it were something like:

> cat char_with_binds.sh
sqlplus -s scott/tiger << EOF
variable my_var varchar2(100)
begin :my_var := '$1'; end;
/
select :my_var with_binds from dual;
exit
EOF


> char_with_binds.sh "Hello World"

PL/SQL procedure successfully completed.


WITH_BINDS
---------------------------------------------------------
Hello World

Othewise it would be breaking your Mantra n'est pas? 'Always use bind variables.'

Regards

Ian



Tom Kyte
June 21, 2004 - 8:41 am UTC

begin :my_var := '$1'; end;
/

would generate just as many unique SQL's. that plsql block would be hard parsed.

a script like this would benefit from

alter session set cursor_sharing=force;


if the input value changes (if the environment variable is constantly changing)

Doh!

Ian, June 21, 2004 - 1:14 pm UTC

That will teach me! Obvious now you point it out.

Just proves that sometimes when you test you see the result you want to see. I was happily proving to myself that there was only one statement in v$sqlarea with the with_binds string - completely missed all the begin :my_var := whatever statements.

Thanks for putting me right - will try again with alter session set cursor_sharing=force;

Thanks and Regards

Ian


More to Explore

VLDB

If you are new to partitioning, check out Connor McDonald's introduction series here.

VLDB

Documentation set on VLDB and Partitioning.