如何编写Oracle过程以基于传递的arrray返回两个游标

时间:2015-08-17 06:34:19

标签: sql oracle stored-procedures plsql

Requirement: - 我正在尝试编写一个过程,它会将一组id作为IN参数, 和两个输出游标作为参数。基本上该过程应循环输入数组并检查是否存在id 比如表Employee,它应该返回两个游标

a)第一个游标 - 包含所有出现的id b)第二个光标 - 包含所有不存在的ID

请帮我解决这个问题

创建了员工表: -

 CREATE TABLE EMPLOYEE
(ID NUMBER(10));

在其中插入ID: -

INSERT INTO EMPLOYEE VALUES(1);
INSERT INTO EMPLOYEE VALUES(2);
INSERT INTO EMPLOYEE VALUES(3);
INSERT INTO EMPLOYEE VALUES(4);
INSERT INTO EMPLOYEE VALUES(5);
INSERT INTO EMPLOYEE VALUES(6);
INSERT INTO EMPLOYEE VALUES(7);
INSERT INTO EMPLOYEE VALUES(9);
INSERT INTO EMPLOYEE VALUES(10);
INSERT INTO EMPLOYEE VALUES(11);

创建自定义输入数组: -

CREATE OR REPLACE TYPE VALID_ID AS VARRAY(500) OF NUMBER(10);

程序声明是这样的: -

CREATE OR REPLACE PROCEDURE VALIDATE_ID(IN_ARR IN VALID_ID,PRSNT_ID OUT  SYS_REFCURSOR,MISS_ID OUT SYS_REFCURSOR)

如果输入数组到proc包含: - 1,2,3,4,5,6,7,8,9,12,13
然后PRSNT_ID应包含: - 1,2,3,4,5,6,7,9,即员工表中的ids 和MISS_ID应包含: - 8,12,13,即员工表中缺少的ID

1 个答案:

答案 0 :(得分:0)

create or replace procedure cursro_retur(arr in VALID_ID, pc1 out sys_refcursor, pc2 out sys_refcursor) is 
begin 
 open pc1  for 'select id from EMPLOYEE t ,table(:arr) b where t.id = b.column_value' using arr; -- valid
 open pc2 for 'SELECT b.column_value FROM employee a,table(:arr) b
 where  a.id (+) = b.column_value 
  and a.id is null' using arr;
end;

declare 
  pc1 sys_refcursor;
  pc2 sys_refcursor;
  v_list1  VALID_ID;
  v_list2  VALID_ID;
begin 
 cursro_retur( VALID_ID(1,2,3,8,11,89,11),pc1,pc2);

fetch pc1 bulk collect into v_list1;
fetch pc2 bulk collect  into v_list2;
 close pc1;
 close pc2; 
 dbms_output.put_line('valid');
 for rec in v_list1.first ..v_list1.last loop
  dbms_output.put_line(v_list1(rec));
 end loop; 
 dbms_output.put_line('no valid');
  for rec in v_list2.first ..v_list2.last loop
  dbms_output.put_line(v_list2(rec));
 end loop; 
end;