0

如何在 SQLPlus 中运行路径名以字符串形式传递的脚本。类似于以下内容(失败):

SET SERVEROUTPUT ON;
DECLARE
   path VARCHAR2(128);
BEGIN
   path := '<runtime_path>' || 'test_script.sql';
   dbms_output.put_line(path);
   @ path;
END;
/
4

4 回答 4

2

您可以将参数传递给 SQLPlus,但不能将 PL/SQL 与 SQLPlus 命令混合使用。所以你的例子不会飞。所以你可能需要用一个 shell 脚本来包装它。但是当我查看您的 PL/SQL 例程时,您只需尝试添加路径。也许这是可能的。

像这样调用sqlplus

sqlplus user/password@database @genericscript.sql path

然后在 genericscript.sql

SET SERVEROUTPUT ON
start &1.myscript.sql
quit

在我的例子中 myscript.sql 有这个内容

select 'hello welt' from dual;

所以我从 SQLPlus 得到了以下输出

[oracle@localhost sqp]$ sqlplus user/pw@db @genericscript.sql /home/oracle/sqp/

SQL*Plus: Release 11.2.0.2.0 Production on Tue May 14 22:53:15 2013

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


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options


'HELLOWELT
----------
hello welt

Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

希望能帮助到你。您可以查看 SQLPLUS 参考手册。

http://docs.oracle.com/cd/E11882_01/server.112/e16604/ch_five.htm#autoId13

我更喜欢PDF

http://docs.oracle.com/cd/E11882_01/server.112/e16604.pdf

于 2013-05-14T20:30:31.163 回答
1

一种选择:

D:\>type run.sql
col p new_value path noprint
select '&1.' as p from dual
/

@&path
D:\>type run1.sql
select 'This is run1' from dual
/
D:\>sqlplus hr/hr@sandbox @run.sql d:\run1.sql

SQL*Plus: Release 11.2.0.1.0 Production on Wed May 15 13:24:22 2013

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


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production




Elapsed: 00:00:00.03

'THISISRUN1'
------------
This is run1

Elapsed: 00:00:00.01

你也可以在匿名块中内联脚本,非常方便:

D:\>sqlplus hr/hr@sandbox @parent.sql d:\child.sql

SQL*Plus: Release 11.2.0.1.0 Production on Wed May 15 13:31:46 2013

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


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production

currently spooling to S:\spool\sandbox\20130515_1331_HR_33.log
this is child script called from parent's anonymous plsql block
child's name was passed using command line parameter

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.00
13:31:47 HR@sandbox> get parent
  1  begin
  2  @&1.
  3* end;
13:31:48   4  .
13:31:48 HR@sandbox> get child
  1  dbms_output.put_line('this is child script called from parent''s anonymous plsql block');
  2* dbms_output.put_line('child''s name was passed using command line parameter');
13:31:50 HR@sandbox>
于 2013-05-15T05:25:54.487 回答
0

我在 Windows 上执行此操作的方式是创建一个 BAT 文件,其中包含:

sqlplus my_username/my_password@tns.test.company.com %*

然后从命令提示符执行文件的 SQL 内容,我只需键入:

batfile.bat @myfile.sql
于 2013-05-14T20:17:03.047 回答
0

从 PL/SQL 运行可执行文件的唯一方法是动态创建调度程序类型EXECUTABLE并调度作业。

于 2013-05-15T07:25:04.800 回答