I wish to execute DMLs using bind variable with shell scripts. For example, something like:
#!/bin/bash
SH_NUM=10
sqlplus -S test_user/test_pass <<EOD
var a number;
a:=${SH_NUM}
insert into test_table values(a);
commit;
EOD
I'm not sure if this is possible, I can use this approch when using pl/sql but I wish to know if I can do it this method as well.
asked Nov 5, 2018 at 15:59
1 Answer 1
Sure it is possible:
[oracle@o71 ~]$ cat 1.sh
#!/bin/bash
SH_NUM=10
sqlplus -S bp/bp<<EOD
set lines 220 pages 5000
col plan_table_output format a50
var a number;
exec :a :=${SH_NUM};
insert into t1 values(:a);
commit;
select * from table(dbms_xplan.display_cursor(format=>'basic'));
exit
EOD
Execute it (I just put DBMS_XPLAN.DISPLAY_CURSOR
in there to see the last statement executed):
[oracle@o71 ~]$ ./1.sh
1 row created.
Commit complete.
PLAN_TABLE_OUTPUT
--------------------------------------------------
EXPLAINED SQL STATEMENT:
------------------------
insert into t1 values(:a)
-----------------------------------------
| Id | Operation | Name |
-----------------------------------------
| 0 | INSERT STATEMENT | |
| 1 | LOAD TABLE CONVENTIONAL | T1 |
-----------------------------------------
12 rows selected.
answered Nov 5, 2018 at 16:05
-
Well, it does completed without error, however
select * from t1
returns 0 rows..Nir– Nir2018年11月05日 16:17:34 +00:00Commented Nov 5, 2018 at 16:17 -
@Nir I beg to differ: pastebin.com/Ny7gWh7gBalazs Papp– Balazs Papp2018年11月05日 16:49:31 +00:00Commented Nov 5, 2018 at 16:49
-
Can you
select *
and notcount(*)
as values are sent empty. I double checked and row count does increasing but values are empty string (or null).Nir– Nir2018年11月05日 16:58:15 +00:00Commented Nov 5, 2018 at 16:58 -
Solution: change
a
assignment toexec :a := ${SH_NUM};
Nir– Nir2018年11月05日 17:09:16 +00:00Commented Nov 5, 2018 at 17:09 -
I think
a=${SH_NUM}
will not do what you expect becausea
is the append command of the sqlplus editor. The correct command isexec :a:=3
orbegin :a:=3; end;
miracle173– miracle1732018年11月05日 17:09:32 +00:00Commented Nov 5, 2018 at 17:09
lang-sql