假设表EMP有三列(deptno,ename和salary)。 我将如何编写一个看起来像这样的for循环并能够访问LISTAGG函数中的项目
BEGIN
FOR rec IN (SELECT deptno, LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename) AS employees
FROM emp
WHERE deptNo = 'XYZ'
GROUP BY deptno)
LOOP
Update EMP
set salary = 10000
WHERE ename in (THE ITEMS IN THE LISTAGG)
FOR EACH (ITEM RETURNED IN THE LISTAGG)
DO SOMETHING
END;
END LOOP;
END;
基本上我想知道两件事,
提前致谢。
答案 0 :(得分:1)
以下是OP请求的示例应用程序:
BEGIN
FOR rec IN (SELECT deptno, LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename) AS employees
FROM emp
WHERE deptNo = 'XYZ'
GROUP BY deptno)
LOOP
Update EMP
set salary = 10000
WHERE ename in
( select regexp_substr(rec.employees,'[^,]+',1,level)
from dual
connect by level <= length(regexp_replace(rec.employees,'[^,]+')) + 1
)
AND deptNo=rec.deptNo; --not to miss this filter condition
FOR EACH (ITEM RETURNED IN THE LISTAGG)
DO SOMETHING
END;
END LOOP;
END;
要查看解析分隔字符串列表的另一个工作示例,请参阅Stack Overflow以了解涉及delimited string parsing routine的类似问题。