中易网

sqlplus 执行脚本文件时如何传参数

答案:1  悬赏:50  
解决时间 2021-01-15 23:37
  • 提问者网友:两耳就是菩提
  • 2021-01-15 08:56
sqlplus 执行脚本文件时如何传参数
最佳答案
  • 二级知识专家网友:刀戟声无边
  • 2021-01-15 09:13
d:\test.sql脚本如下:
[sql] view plaincopy
select &1 from &2;
exit;

执行时这样传参数:sqlplus "scott/tiger@test" @d:\test.sql sysdate dual
注意:参数必须用&[1-9]表示,不然传不进去,会提示让手动输入参数
[sql] view plaincopy
C:\>sqlplus "scott/tiger@test" @d:\test.sql sysdate dual

D:\>sqlplus "scott/tiger@test" @d:\test.sql sysdate dual

SQL*Plus: Release 11.2.0.1.0 Production on 星期二 11月 1 21:59:00 2011

Copyright (c) 1982, 2010, Oracle.All rights reserved.

连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

原值1: select &1 from &2
新值1: select sysdate from dual

SYSDATE
--------------
01-11月-11

从 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options 断


D:\>

如果脚本中有重复用到相同的值,如果&1=&2:
d:\tes2.sql
[sql] view plaincopy
delete scott.emp where no=&1or deptno=&2;
commit;

执行时,就必须传2个参数:
[sql] view plaincopy
sqlplus "scott/tiger@test" @d:\test2.sql 10 10

小窍门: 这时用procedure就可以不用传多个相同的参数,则只用传1个参数:
d:\test3.sql
[sql] view plaincopy
declare
var_no number:=&1;
begin
delete scott.emp where no=var_no or deptno=var_no;
commit;
end;

sqlplus "scott/tiger@test" @d:\test3.sql 10
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息