我有一个包含以下内容的sql文件: 请注意,v $ database中的CDB列仅在版本(不是Oracle而是其他一些不相关的产品)大于11时才存在。
当我使用sqlplus在数据库主机上执行它时运行正常 sqlplus / as sysdba @ abc.sql 1000 60 10
但是,当我使用连接字符串远程连接到此数据库主机时,如果CDB列不存在(无论我指定的是什么版本值),它会抱怨“无效标识符”
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage
Management, Oracle Label Security,
OLAP, Data Mining, Oracle Database Vault and Real Application Testing options
.
old 2: num_ret_period NUMBER(9) :=
new 2: num_ret_period NUMBER(9) := 43200;
old 3: num_interval NUMBER(9) :=
new 3: num_interval NUMBER(9) := 60;
old 5: lcm_version NUMBER(4) :=
new 5: lcm_version NUMBER(4) := 1;
SELECT CDB INTO isCDB from v$database;
*
ERROR at line 10:
ORA-06550: line 10, column 12:
PL/SQL: ORA-00904: "CDB": invalid identifier
ORA-06550: line 10, column 5:
PL/SQL: SQL Statement ignored
任何想法为什么?
DECLARE
num_ret_period NUMBER(9) := &1;
num_interval NUMBER(9) := &2;
isCDB VARCHAR2(10);
version NUMBER(4) := &3;
BEGIN
IF version > 11 THEN
SELECT CDB INTO isCDB from v$database;
IF (isCDB = 'YES') THEN
EXECUTE IMMEDIATE 'ALTER session SET CONTAINER = CDB$ROOT';
dbms_output.put_line('CDB is set to YES');
ELSE
dbms_output.put_line('CDB is set to NO');
END IF;
END IF;
callSomeProcedure();
commit;
EXCEPTION
when others then
dbms_output.put_line('There is an exception in abc.sql' || SQLCODE || ' ' || SQLERRM);
ROLLBACK;
raise_application_error(-20001, 'There is an exception in abc.sql' || SQLCODE || ' ' || SQLERRM);
END;
/
答案 0 :(得分:3)
即使您不打算执行由if version > 11
保护的代码,仍然需要解析它。此时显示CDB
不存在(版本11)的事实。
为避免这种情况,请使用条件编译:
$IF DBMS_DB_VERSION.VER_LE_11 $THEN
-- version 11 code
NULL;
$ELSE
-- version 12 and later code
NULL;
$END
条件编译指令的作用类似于预处理程序。
请参阅https://oracle-base.com/articles/10g/conditional-compilation-10gr2