oracle PL/SQL ORA-00902:无效的数据类型

eulz3vhy  于 2023-11-17  发布在  Oracle
关注(0)|答案(1)|浏览(448)

我试图把这段代码到一个商店proc。但更新语句抛出无效的数据类型错误。任何帮助或建议将不胜感激。

CREATE TABLE "TABLE1" 
   (    "USER_ID" NUMBER, 
    "COMMENTS" VARCHAR2(20 BYTE)
   )

PROCEDURE pr_test
AS
v_table UserRecordTable;
v_user_id_list SYS.ODCINUMBERLIST;
BEGIN

select USER_ID bulk collect into v_table 
    from  TABLE1;

select USER_ID bulk collect into v_user_id_list 
from TABLE1 
where USER_ID in (select USERID FROM TABLE(v_table)); -- No problem

FOR i IN 1..v_user_id_list.COUNT LOOP
    DBMS_OUTPUT.PUT_LINE('Column1: ' || v_user_id_list(i)); -- printout ok
END LOOP;

UPDATE TABLE1    -- This line throws the error
SET COMMENTS= 'NO NO'
where USER_ID in (select USERID FROM TABLE(v_table));
END pr_der_test;

字符串
符合规格

TYPE UserRecordType IS RECORD (
    USERID TABLE1.USER_ID%TYPE
  ); 
TYPE UserRecordTable IS TABLE OF UserRecordType;

uelo1irk

uelo1irk1#

这个问题可能与记录是PL/SQL数据类型有关,并且它们的支持在SQL语句中受到限制(但在最近的版本中有所增加)。
您可以在查询中使用v_user_id_list(它使用SYS.ODCINUMBERLIST,一种在SQL作用域中而不是在包的PL/SQL作用域中声明的数据类型)来解决这个问题。

CREATE PACKAGE package_name
AS 
  TYPE UserRecordType IS RECORD (USERID TABLE1.USER_ID%TYPE); 
  TYPE UserRecordTable IS TABLE OF UserRecordType;
  PROCEDURE pr_test;
END;
/

CREATE PACKAGE BODY package_name
AS 
  PROCEDURE pr_test
  AS
    v_table UserRecordTable;
    v_user_id_list SYS.ODCINUMBERLIST;
  BEGIN
    select USER_ID
    bulk collect into v_table 
    from  TABLE1;

    select USER_ID
    bulk collect into v_user_id_list 
    from TABLE1 
    where USER_ID in (select USERID FROM TABLE(v_table)); -- No problem

    FOR i IN 1..v_user_id_list.COUNT LOOP
      DBMS_OUTPUT.PUT_LINE('Column1: ' || v_user_id_list(i)); -- printout ok
    END LOOP;

    UPDATE TABLE1    -- This line throws the error
    SET COMMENTS= 'NO NO'
    -- where USER_ID in (select USERID FROM TABLE(v_table))
    where USER_ID in (select COLUMN_VALUE FROM TABLE(v_user_id_list))
    ;
  END;
END;
/

字符串
给定样本数据:

CREATE TABLE table1(
  user_id  NUMBER,
  comments VARCHAR2(20)
);

INSERT INTO table1 (user_id, comments)
SELECT 1, NULL FROM DUAL UNION ALL
SELECT 2, 'YES' FROM DUAL;


然后:

BEGIN
  DBMS_OUTPUT.ENABLE();
  package_name.pr_test;
END;
/


产出:

Column1: 1
Column1: 2


然后,该表包含:
| 用户ID|评论|
| --|--|
| 1 |不不|
| 2 |不不|
fiddle

相关问题