Use Bulk Collect result in a select query without cursor

一个人想着一个人 提交于 2019-12-08 11:47:57

问题


I am new to PL/SQL and I was wondering if I can use result of a bulk collect like this:

Declare
type result_bulk_type is Table of table1.ID%type;
result_bulk result_bulk_type;
BEGIN
SELECT id BULK COLLECT INTO result_bulk FROM table1;
UPDATE table2 SET status=1 WHERE id IN result_bulk;
END;

I got errors at compilation:

PL/SQL: SQL statement ignored

PL/SQL: ORA-00932: inconsistent datatypes: expected NUMBER got SYS_PLSQL_22223_23_1

Thanks for your help!


回答1:


No, it can't be done in this way. Use FORALL statement instead.
Read this: http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/forall_statement.htm#LNPLS01321

An example:

Declare
  type result_bulk_type is Table of table1.ID%type;
  result_bulk result_bulk_type;
BEGIN
  SELECT id BULK COLLECT INTO result_bulk FROM table1;
  FORALL i IN INDICES OF result_bulk
    UPDATE table2 SET status=1 WHERE id = result_bulk( i );
END;
/

demo: http://sqlfiddle.com/#!4/b3a72/1




回答2:


Use 'WHERE id MEMBER OF result_bulk'



来源:https://stackoverflow.com/questions/21755784/use-bulk-collect-result-in-a-select-query-without-cursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!