【发布时间】:2020-04-28 10:41:41
【问题描述】:
使用 Oracle 12c EE,如何在 DML 子查询中使用 PL/SQL 包类型而不引发异常“ORA-00902:无效数据类型”?
示例架构
--PL/SQL package types.
create or replace package test_pkg as
TYPE type_record IS RECORD(
column1 NUMBER,
column2 NUMBER,
column3 NUMBER);
TYPE type_table IS TABLE OF type_record;
end;
/
--For comparison, the same types but as SQL objects.
CREATE OR REPLACE TYPE type_record IS OBJECT(
column1 NUMBER,
column2 NUMBER,
column3 NUMBER);
CREATE OR REPLACE TYPE type_table IS TABLE OF type_record;
--Table for testing DML.
create table tableX(a number);
SQL SELECT 中的 PL/SQL 类型 - WORKS
从 PL/SQL 类型到 SQL 的转换适用于 SELECTS。下面的代码运行良好:
declare
vt test_pkg.type_table;
v_count number;
begin
select count(*)
into v_count
from dual
where not exists(select column1 from table(vt));
end;
/
SQL 更新中的 PL/SQL 类型 - 失败
但是在 DML 语句中使用相同的类型和子查询会引发异常:“ORA-00902: invalid datatype/ORA-06512: at line 4”。
declare
vt test_pkg.type_table;
begin
update tableX set a = 1
where not exists (select column1 from table(vt));
end;
/
SQL UPDATE 中的 SQL 类型 - WORKS
作为比较,在子查询中使用 SQL 对象在 DML 中效果很好:
declare
vt type_table;
begin
update tableX set a = 1
where not exists (select column1 from table(vt));
end;
/
为每个查询创建 SQL 对象是一种解决方法,但这会创建很多不必要的架构对象。有没有办法让 PL/SQL 包类型在 DML 子查询中工作?
【问题讨论】:
-
" 我需要停止使用 db 中定义的类型" 为什么?不使用 SQL 类型的驱动因素是什么?
-
公司要求。保持代码干净。
标签: sql oracle plsql oracle12c