从 UPDATE 子查询调用 Oracle 关联数组给出 PLS-00201 错误

Calling Oracle Associative Array from UPDATE subquery gives PLS-00201 error

我正在尝试从关联数组查找值以更新 table,但出现错误。这是演示该问题的最低限度代码(我的实际查询要复杂得多)。

我是不是遗漏了什么,或者如果这是 Oracle 的限制,是否有解决办法?

CREATE TABLE t_employee AS SELECT cast('john' as varchar2(30)) emp_name, CAST (NULL AS NUMBER (5)) emp_id FROM DUAL;
DECLARE
    TYPE emp_typ IS TABLE OF NUMBER INDEX BY VARCHAR2(30);
    emp_lookup   emp_typ;
BEGIN
    emp_lookup ('john') := 1234;
    UPDATE t_employee e
       SET emp_id = (SELECT emp_lookup (e.emp_name) FROM DUAL);
END;
/

--DROP TABLE t_employee;

> PLS-00201:必须声明标识符 'E.EMP_NAME'

当我将更新更改为更简单的构造时:

UPDATE t_employee SET emp_id = emp_lookup (t_employee.emp_name);

我发现这个问题:PLS-00382:表达式类型错误

同SELECT。在我看来,我们不能直接在 SQL 语句中使用关联数组。

关联数组集合是 PL/SQL 唯一的数据类型;它不能在 SQL 语句中使用。

来自Oracle documentation

Table 5-1 PL/SQL: Collection Types

Collection Type Number of Elements Index Type Dense or Sparse Uninitialized Status Where Defined Can Be ADT Attribute Data Type
Associative array (or index-by table) Unspecified String or PLS_INTEGER Either Empty In PL/SQL block or package No
VARRAY (variable-size array) Specified Integer Always dense Null In PL/SQL block or package or at schema level Only if defined at schema level
Nested table Unspecified Integer Starts dense, can become sparse Null In PL/SQL block or package or at schema level Only if defined at schema level

...

An associative array (formerly called PL/SQL table or index-by table) is a set of key-value pairs.

...

Unlike a database table, an associative array:

  • Does not need disk space or network operations
  • Cannot be manipulated with DML statements

VARRAY 和嵌套的 table 集合可以在 SQL 语句中使用。