PL / SQL:ORA-00907:批量收集错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PL / SQL:ORA-00907:批量收集错误相关的知识,希望对你有一定的参考价值。
有没有办法我们使用distinct
和别名(as
)来实现批量收集查询,如下所示。
SELECT distinct OBJ_TEST ( EMP_ID as E1, EMP_NAME)
BULK COLLECT INTO LVOB_TEST
FROM TMP_EMP ;
答案
有没有办法我们使用distinct&alias(as)来实现批量收集查询,如下所示。
不确定为什么要使用别名,因为在bulk collect
中使用别名并使用它是没有意义的。您将始终必须使用您定义的对象的column names
。请参阅下文并阅读我的内联评论:
CREATE OR REPLACE TYPE OBJ_TEST AS OBJECT ( EMP_ID NUMBER(10), EMP_NAME VARCHAR2(100) ) ;
/
CREATE OR REPLACE TYPE V_OBJ_TYP IS TABLE OF OBJ_TEST;
/
CREATE TABLE TMP_EMP ( EMP_ID NUMBER(10), EMP_NAME VARCHAR2(100) );
/
INSERT INTO TMP_EMP VALUES (1, 'XXX');
INSERT INTO TMP_EMP VALUES (2, 'HHH');
INSERT INTO TMP_EMP VALUES (3, 'ZZZ');
INSERT INTO TMP_EMP VALUES (4, 'YYY');
/
COMMIT;
/
DECLARE
LVOB_TEST V_OBJ_TYP;
BEGIN
--See below how you can use the alias but it doesnot make sense
--since you cannot use the alias name while displaying the result using alias name.
SELECT OBJ_TEST (EP_ID, E_NAME)
BULK COLLECT INTO LVOB_TEST
FROM (SELECT DISTINCT EMP_ID AS EP_ID,
EMP_NAME AS E_NAME
FROM TMP_EMP);
DBMS_OUTPUT.PUT_LINE ('EMP_ID'||'--' ||'EMP_NAME');
FOR I IN 1 .. LVOB_TEST.COUNT
LOOP
--Here am displaying the result of the query. But i cannot use alias since i need can only refer the name of the columns defined in the Object. i.e. EMP_ID & EMP_NAME.
--IF you want to use that alias then create the object with that alias name.
DBMS_OUTPUT.PUT_LINE (LVOB_TEST (I).EMP_ID ||'--' ||LVOB_TEST (I).EMP_NAME );
END LOOP;
END;
输出:
SQL> /
EMP_ID--EMP_NAME
4--YYY
2--HHH
3--ZZZ
1--XXX
PL/SQL procedure successfully completed.
另一答案
你对细节非常了解,但猜测一下,你想:
SELECT distinct OBJ_TEST ( EMP_ID , EMP_NAME)
BULK COLLECT INTO LVOB_TEST
from
( select distinct emp_id, emp_name from tmp_emp )
以上是关于PL / SQL:ORA-00907:批量收集错误的主要内容,如果未能解决你的问题,请参考以下文章
SQL 错误:ORA-00907:缺少右括号 00907。00000 -“缺少右括号”