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!

有帮助吗?

解决方案

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

其他提示

Use 'WHERE id MEMBER OF result_bulk'

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top