感谢您的回答。您能否添加一个示例代码以从 v_emp 集合中删除计数为零的条目?
从您的评论中挑选,您应该只选择查询中计数不为零的那些记录,而不是在稍后阶段检查它。见下文。
CREATE TABLE emp(emp_NM VARCHAR2(10),
emp_id NUMBER,
sal NUMBER);
/
INSERT INTO emp VALUES ('X',1,100);
INSERT INTO emp VALUES ('A',2,200);
INSERT INTO emp VALUES('B',3,300);
INSERT INTO emp values ('C',4,400);
/
Select * from emp;
/
CREATE OR REPLACE TYPE ep_id IS TABLE OF NUMBER;
/
DECLARE
Type v_emp IS TABLE OF NUMBER INDEX BY pls_integer;
emp_var v_emp;
--Declared a table having emp_id as in your case where you try to get this in a collection v_emp.
v_ep_id ep_id:=ep_id(1,2,4,5,6);
BEGIN
---Query to select only those records whose count is greater than 0.
SELECT COUNT(emp_id)
bulk collect INTO emp_var
FROM emp
--WHERE emp_id IN (SELECT * FROM TABLE(v_ep_id) ) --< You can use this as well.
WHERE emp_id MEMBER OF v_ep_id
GROUP BY emp_id, sal, emp_nm
HAVING COUNT(emp_id) > 0 ;
--Here you directly do the merge of the selected records which are not Zero
FORALL i IN 1 .. emp_var.Count
MERGE INTO emp1 e1 using dual ON(a.id=b.id)
WHEN MATCHED
//Update statement
WHEN NOT MATCHED
//Insert statement
END IF;
Exception
WHEN others then
raise_application_error(-20001,'Error');
END;