[英]PL SQL - Deleting rows from table based on the ID in a collection
I have an issue I'm hoping someone could assist me with. 我有一个问题,我希望有人可以帮助我。 My goal is to insert several rows based on some conditions on different tables, and after that delete those rows from the original tables.
我的目标是根据不同表上的某些条件插入几行,然后从原始表中删除这些行。
So far I have succeeded in inserting, but for deleting I'm having some difficulties. 到目前为止,我已成功插入,但删除我遇到了一些困难。 Now I'm trying to store the ID's of the rows to be inserted in some collection, so I can delete the rows with those ID's on the DELETE operation.
现在我正在尝试存储要插入到某些集合中的行的ID,因此我可以在DELETE操作中删除具有这些ID的行。
Code example: 代码示例:
... ...
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;
The error I'm getting is : "local collection types not allowed in SQL statements". 我得到的错误是:“SQL语句中不允许使用本地集合类型”。 I've tried reading into this error but still couldn't understand what the best solution would be to the issue.
我已经尝试过阅读此错误,但仍然无法理解该问题的最佳解决方案。
Thanks, Best regards. 谢谢,最好的问候。
You have almost got it! 你几乎得到了它! Just several places to point out: 1) declare the type globally, not in the inner anonymous block, like :
只需指出几个地方:1)全局声明类型,而不是内部匿名块,如:
create or replace type array_type is varry(100000) of integer;
2) the final code is: 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.