我有一个问题,希望有人可以帮助我。 我的目标是根据某些条件在不同的表上插入几行,然后从原始表中删除这些行。
到目前为止,我已成功插入,但是删除时遇到了一些困难。现在,我试图将要插入的行的ID存储在某个集合中,以便可以在DELETE操作中删除具有这些ID的行。
代码示例:
...
TYPE array_type IS VARRAY(100000) OF INTEGER;
array_SYSID array_type;
total_ids INTEGER;
BEGIN
SELECT a1.SYS_ID BULK COLLECT INTO array_SYSID FROM NOCCIA_TICKET_JOURNEY_CONTROL a1 WHERE (((CAST(a1.SYS_CREATED_ON as DATE) < CAST(archive_date as DATE) AND INSTR(LISTOFFINALSTATES, a1.U_JOURNEY_STATUS) > 0 )) AND a1.SYS_ID NOT IN (SELECT b1.U_TICKET_JOURNEY_SYSID FROM NOCCIA_AUTOMATION_CONTROL b1 WHERE b1.U_STATUS LIKE '%In Automation'));
total_ids := array_SYSID.count;
FOR i in 1 .. total_ids LOOP
dbms_output.put_line(array_SYSID(i));
END LOOP;
DELETE FROM NOCCIA_TICKET_JOURNEY_CONTROL
WHERE SYS_ID IN array_SYSID;
我得到的错误是:“ SQL语句中不允许使用本地集合类型”。我已经尝试阅读此错误,但仍然不知道该问题的最佳解决方案是什么。
谢谢, 最好的问候。
答案 0 :(得分:3)
您可以使用FORALL
:
...
forall i in array_SYSID.first .. array_SYSID.last
DELETE FROM NOCCIA_TICKET_JOURNEY_CONTROL
WHERE SYS_ID= array_SYSID(i);
...
答案 1 :(得分:0)
您几乎掌握了!请指出几个地方: 1)全局声明类型,而不是在内部匿名块中声明,例如:
create or replace type array_type is varry(100000) of integer;
2)最终代码是:
declare
array_SYSID array_type;
total_ids INTEGER;
BEGIN
SELECT
a1.SYS_ID
BULK COLLECT INTO
array_SYSID
FROM
NOCCIA_TICKET_JOURNEY_CONTROL a1
WHERE(((
CAST(a1.SYS_CREATED_ON as DATE) < CAST(archive_date as DATE) AND
INSTR(LISTOFFINALSTATES, a1.U_JOURNEY_STATUS) > 0 )) AND
a1.SYS_ID NOT IN(
SELECT
b1.U_TICKET_JOURNEY_SYSID
FROM
NOCCIA_AUTOMATION_CONTROL b1
WHERE
b1.U_STATUS LIKE '%In Automation'));
total_ids := array_SYSID.count;
FOR i in 1 .. total_ids LOOP
dbms_output.put_line(array_SYSID(i));
END LOOP;
DELETE FROM NOCCIA_TICKET_JOURNEY_CONTROL
WHERE SYS_ID IN(
select
*
from
table(array_SYSID);
end;