Oracle

編譯通過 shell 腳本傳遞給 sqlplus 會話的 PLSQL 包程式碼?

  • August 24, 2016

我有兩個文件:(1)和(2)

(1) 一個Shell腳本——wrapper.sh

#!/bin/sh
echo "start of shell script"

FILE_NAME="XX_PKGb.sql"

sqlplus user/password @sql_deploy.sql $FILE_NAME

echo "end of shell script."

exit $RETURN_CODE

(2) 一個plsql腳本——sql_deploy.sql

set serveroutput on

set term on

set echo off

set feedback off

set define on

set verify off


define SQL_SCRIPT=&1


declare

 v_sql            varchar2(240);

begin

   v_sql := 'ALTER SESSION SET CURRENT_SCHEMA = XXEOM';

     --alter the schema (this works)

     EXECUTE IMMEDIATE v_sql; 

     --compile the package (this throws an error)  

     @@'&SQL_SCRIPT';    

end;
/


exit;

該文件XX_PKGb.sql也存在於與上述兩個文件相同的位置。當我執行 shell 腳本時,我看到sqlplus會話執行sql_deploy.sql腳本,它能夠更改架構,但是,在編譯包時,它拋出了以下錯誤。請問如何編譯這個sql文件中的一段程式碼:

CREATE OR REPLACE PACKAGE BODY XXG_PKG
*
ERROR at line 15:
ORA-06550: line 15, column 1:
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
( begin case declare end exception exit for goto if loop mod
null pragma raise return select update while with
<an identifier> <a double-quoted delimited-identifier>
<a bind variable> << continue close current delete fetch lock
insert open rollback savepoint set sql execute commit forall
merge pipe purge

不允許使用 PL/SQL 塊執行 XX_PKGb.sql(包創建語句)。如果你必須在那裡做,那麼它需要是動態 SQL。

更好的方法是簡單地從 SQLPlus 執行它;

set term on

set echo off

set feedback off

set define on

set verify off

ALTER SESSION SET CURRENT_SCHEMA = XXEOM      
/
@XX_PKGb.sql
/
exit

不能從過程塊中呼叫外部 SQL 腳本。您正在嘗試向 PL/SQL 編譯器發出 SQLPlus 命令。你明白為什麼這行不通嗎?

相反,請嘗試使用這種格式將普通命令通過管道傳輸到您的 shell 腳本中的 SQLPlus:

sqlplus / as sysdba << EOF

set serveroutput on 
set term on 
set echo off  
set feedback off  
set define on 
set verify off

ALTER SESSION SET CURRENT_SCHEMA = XXEOM;

@XX_PKGb.sql;

EOF

引用自:https://dba.stackexchange.com/questions/147667