Home » Infrastructure » Unix » Passing String Arguments to SQL*Plus from a Unix Shell
Passing String Arguments to SQL*Plus from a Unix Shell [message #227569] |
Wed, 28 March 2007 12:16 |
ukuechle
Messages: 9 Registered: June 2005
|
Junior Member |
|
|
Hi all,
I've scanned the FAQ and Forums but haven't found a satisfying answer so far:
I need to pass arguments containing the SQL string delimiter "'" to sqlplus from a shell script, e.g.:
sqlplus user/pass @my.sql $1
where $1 should be like "'1','2','3'", so that it can be resolved to
...
WHERE mycol NOT IN ( '1','2','3' );
I know how to pass arguments/parameters without the "'", but when I use "'" it gets erased by the shell. I've tried to escape the "'" but with no success so far.
It looks so simple but I can't find a solution. *help*
Regards,
Uwe
|
|
|
|
Re: Passing String Arguments to SQL*Plus from a Unix Shell [message #227619 is a reply to message #227575] |
Wed, 28 March 2007 15:54 |
andrew again
Messages: 2577 Registered: March 2000
|
Senior Member |
|
|
You can always avoid the pesky quotes until runtime. Substitute them with some other "non-special" character (that also won't occur in your data).
dev>>cat t.sql
set echo on
set verify on
select replace('&1', '^', '''') from dual;
dev>>export X=^1^,^2^,^3^
dev>>sqlplus tst/tst1@dev1 @t.sql $X
SQL*Plus: Release 9.2.0.6.0 - Production on Wed Mar 28 13:51:19 2007
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
With the Partitioning option
JServer Release 9.2.0.8.0 - Production
SQL> set verify on
SQL> select replace('&1', '^', '''') from dual;
old 1: select replace('&1', '^', '''') from dual
new 1: select replace('^1^,^2^,^3^', '^', '''') from dual
REPLACE('^1
-----------
'1','2','3'
SQL>
|
|
|
Re: Passing String Arguments to SQL*Plus from a Unix Shell [message #227730 is a reply to message #227619] |
Thu, 29 March 2007 03:44 |
ukuechle
Messages: 9 Registered: June 2005
|
Junior Member |
|
|
Thanks for your suggestion, andrew.
Though your example works, it is not fully suitable to my problem: I do not select with a possibility to replace the strings, instead I want to modify the where clause in an existing statement.
It works fine as long as I am not in need of quote characters:
define inlist=&1
...
SELECT something FROM mytable
WHERE something NOT IN ( &inlist );
Now I could pass "1,2,3" as an argument but not "'1','2','3'". This is the crucial point.
Regards,
Uwe
|
|
|
|
|
Re: Passing String Arguments to SQL*Plus from a Unix Shell [message #227828 is a reply to message #227762] |
Thu, 29 March 2007 10:04 |
tahpush
Messages: 961 Registered: August 2006 Location: Stockholm/Sweden
|
Senior Member |
|
|
Just an simple example to clarify
vi test.sh
#!/bin/sh
echo 'enter input:'
read inpar
l_var=`echo ${inpar} | sed "s/'//g" `
echo 'new value:' $l_var
run test.sh
[/] . test.sh
enter input:
"1,2,3"
new value: "1,2,3"
[/] . test.sh
enter input:
"'1','2','3'"
new value: "1,2,3"
So if you put in either "1,2,3" or "'1','2','3'"
you get the same result back, and then pass it on
|
|
|
Re: Passing String Arguments to SQL*Plus from a Unix Shell [message #227832 is a reply to message #227828] |
Thu, 29 March 2007 11:11 |
ukuechle
Messages: 9 Registered: June 2005
|
Junior Member |
|
|
tahpush,
thanks for the example. Unfortunately, SQL*Plus filters quotes as well, which leads to the now passed characters to be stripped anyway.
But I was provided with another idea to split up the script in a definition part and an execution part and do the definition within the shell script like this:
$ cat test.sql
select sysdate from dual
where dummy not in( &list1 );
exit
$ sqlplus -s user/pass <<END
> define list1="'1','2'"
> @test
> exit
> END
SQL> SQL> old 2: where dummy not in( &list1)
new 2: where dummy not in( '1','2' )
Kudos to Nigel Thomas!
Regards,
--==/ Uwe \==--
|
|
|
Re: Passing String Arguments to SQL*Plus from a Unix Shell [message #227841 is a reply to message #227832] |
Thu, 29 March 2007 13:24 |
andrew again
Messages: 2577 Registered: March 2000
|
Senior Member |
|
|
just escape the " and ' at Unix level.
dev>>cat t1.sql
set echo on
set verify on
select * from ABC where col1 in (&1);
dev>>export X=\"\'1\',\'2\',\'3\'\"
dev>>echo $X
"'1','2','3'"
dev>>sqlplus tst/tst1@dev1 @t1.sql $X
SQL*Plus: Release 9.2.0.6.0 - Production on Thu Mar 29 11:20:39 2007
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
With the Partitioning option
JServer Release 9.2.0.8.0 - Production
SQL> set verify on
SQL> select * from ABC where col1 in (&1);
old 1: select * from ABC where col1 in (&1)
new 1: select * from ABC where col1 in ('1','2','3')
COL1 COL2
---------- ----------
1 hello
2 hello2
3 hello3
SQL>
|
|
|
Goto Forum:
Current Time: Sun Nov 24 23:25:57 CST 2024
|