是否可以在存储过程中运行sqlplus文件?

时间:2019-08-29 18:20:16

标签: oracle plsql sqlplus

嗨,可以从存储过程内部运行并执行sqlplus文件吗?

到目前为止,我已经尝试了以下方法:

CREATE OR REPLACE PROCEDURE SCRIPTRUN
(

    p_orgid     IN VARCHAR2,
    p_axtype    IN VARCHAR2,
    P_option    IN VARCHAR2

) AS 

 runn VARCHAR2(200) := '@C:\Scripts\delete_ax\delete-ORG.sql '||  p_orgid ||' '||  p_axtype||' '|| P_option ; 


BEGIN

 execute IMMEDIATE runn;


END SCRIPTRUN;

我收到的错误消息:

ORA-00900:无效的SQL语句 ORA-06512:位于“ DWOLFE.SCRIPTRUN”的第17行 ORA-06512:在第10行

1 个答案:

答案 0 :(得分:0)

1。创建目录EXT_TAB_DATA / opt / oracle / DEV / SAMPLE

 select * from dba_directories
    where directory_name='EXT_TAB_DATA'
    ==>
    OWNER          DIRECTORY_NAME          DIRECTORY_PATH                                                                                       
    --------- ------------------------ ----------------------------------- 
    SYS           EXT_TAB_DATA                /opt/oracle/DEV/SAMPLE              

2。使用sqlplus2预处理器创建外部表sqlplus

DROP TABLE ext_sqlplus
/
CREATE TABLE ext_sqlplus
    (stdout                         VARCHAR2(256 CHAR))
  SEGMENT CREATION IMMEDIATE
  ORGANIZATION EXTERNAL (
   DEFAULT DIRECTORY  EXT_TAB_DATA
    ACCESS PARAMETERS(RECORDS DELIMITED BY NEWLINE
   PREPROCESSOR EXT_TAB_DATA:'sqlplus2'
   BADFILE 'EXT_SQLPLUS_%a_%p.bad'
   NOLOGFILE
   SKIP 1
   FIELDS TERMINATED BY '\n' 
   MISSING FIELD VALUES ARE NULL
 )
   LOCATION (
    EXT_TAB_DATA:'..'
   )
  )
   REJECT LIMIT UNLIMITED
  NOPARALLEL
/

3。使用sqlplus2调用和sql文件sqlplus

创建外壳/opt/oracle/DEV/SAMPLE/select_sysdate_from_dual.sql.
 oracle@esmd:~/DEV/SAMPLE> more sqlplus2 
/oracle/product/11.2.0.3/db/bin/sqlplus /nolog  @/opt/oracle/DEV/SAMPLE/select_sysdate_from_dual.sql

4。创建一个过程write_sqlfile,该过程将查询写入SQL文件select_sysdate_from_dual.sql

CREATE OR REPLACE 
PROCEDURE write_sqlfile (SQL_STRING IN VARCHAR2) IS
 OutFile  utl_file.file_type;


connect_string VARCHAR2(256):='connect system/manageresmd';
exit_string VARCHAR2(25):='exit';
file_name  VARCHAR2(256):='select_sysdate_from_dual.sql';
file_dir   VARCHAR2(25):='EXT_TAB_DATA'; 

BEGIN

  OutFile := utl_file.fopen(file_dir, file_name, 'w');
  utl_file.put_line(OutFile, connect_string, FALSE);
  utl_file.put_line(OutFile, SQL_STRING, FALSE);
  utl_file.put_line(OutFile, exit_string, FALSE);
  utl_file.fflush(OutFile);
  utl_file.fclose(OutFile);

EXCEPTION
  WHEN OTHERS THEN
    RAISE_APPLICATION_ERROR (-20099, 'Unknown UTL_FILE Error');
END write_sqlfile;
/

示例1 运行过程write_sqlfile将sql写入脚本。

begin
write_sqlfile(
'
select ''1-''||to_char(sysdate,''DD-MON-YYYY HH24:MI'') from dual;
select ''2-''||to_char(sysdate,''DD-MON-YYYY HH24:MI'') from dual;
select ''3-''||to_char(sysdate,''DD-MON-YYYY HH24:MI'') from dual;
select ''4-''||to_char(sysdate,''DD-MON-YYYY HH24:MI'') from dual;
select ''5-''||to_char(sysdate,''DD-MON-YYYY HH24:MI'') from dual;
');

end;

检查文件系统中文件的内容。

oracle@esmd:~/DEV/SAMPLE> more select_sysdate_from_dual.sql
connect system/manageresmd

select '1-'||to_char(sysdate,'DD-MON-YYYY HH24:MI') from dual;
select '2-'||to_char(sysdate,'DD-MON-YYYY HH24:MI') from dual;
select '3-'||to_char(sysdate,'DD-MON-YYYY HH24:MI') from dual;
select '4-'||to_char(sysdate,'DD-MON-YYYY HH24:MI') from dual;
select '5-'||to_char(sysdate,'DD-MON-YYYY HH24:MI') from dual;

exit
oracle@esmd:~/DEV/SAMPLE>

执行对外部表的查询,并查看sqlplus输出

SELECT *  FROM ext_sqlplus

输出

    SQL*Plus: Release 11.2.0.3.0 Production on Fri Aug 30 12:49:41 2019

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

Connected.

'1-'||TO_CHAR(SYSDA
-------------------
1-30-AUG-2019 12:49


'2-'||TO_CHAR(SYSDA
-------------------
2-30-AUG-2019 12:49


'3-'||TO_CHAR(SYSDA
-------------------
3-30-AUG-2019 12:49


'4-'||TO_CHAR(SYSDA
-------------------
4-30-AUG-2019 12:49


'5-'||TO_CHAR(SYSDA
-------------------
5-30-AUG-2019 12:49

Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

示例2 运行过程write_sqlfile,将主机命令OS写入脚本。

 begin
    write_sqlfile(
    '
    host  cd /opt ; /bin/ls -l
    ');


    end;

执行对外部表的查询,并查看sqlplus输出

SELECT *  FROM ext_sqlplus;

SQL*Plus: Release 11.2.0.3.0 Production on Fri Aug 30 14:21:27 2019

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

Connected.
total 20
drwxr-xr-x 3 root   root     4096 2012-12-12 10:13 app
drwxr-xr-x 3 root   root     4096 2014-11-10 11:04 IBM
drwxr-xr-x 3 root   root     4096 2012-04-24 09:58 kde3
drwxr-xr-x 8 oracle oinstall 4096 2019-08-13 09:47 oracle
drwxr-xr-x 3 root   root     4096 2012-04-25 11:41 ORCLfmap

Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production