【发布时间】:2021-07-08 14:04:49
【问题描述】:
我想删除我通过以下选择收集的某些行:
DELETE from REMINDER
where exists
(
SELECT r.rowid
FROM reminder r
inner join reminder_users u on u.reminder_id = r.id
inner join device d on d.id = (regexp_replace(r.origin_values, '[^0-9]', '')) and d.NEXT_TEST_DATE_INTERNAL <> u.deadline
inner join device_test t on t.id = d.NEXT_TEST_INT_ID
where u.receipt = 0 and t.TEST_INTERNAL_EXTERNAL = 0 and r.NAME like '%Interne%' and r.NAME not like '%Externe%' and u.DEADLINE > sysdate
);
问题是,有一个表“reminder_users”包含父表“提醒”的子记录。所以我必须先用以下语句删除子记录:
DELETE from REMINDER_USERS
where exists
(
SELECT u.ROWID
from reminder_users u
inner join reminder r on r.id = u.reminder_id
inner join device d on d.id = (regexp_replace(r.origin_values, '[^0-9]', '')) and d.NEXT_TEST_DATE_INTERNAL <> u.deadline
inner join device_test t on t.id = d.NEXT_TEST_INT_ID
where u.receipt = 0 and t.TEST_INTERNAL_EXTERNAL = 0 and r.NAME like '%Interne%' and r.NAME not like '%Externe%' and u.DEADLINE > sysdate
);
当我执行第二个操作时,第一条语句显然返回零行,因为我删除了子记录。这就是为什么我正在寻找一种方法来存储第一次操作中的 rowid,然后删除子记录,最后从表“提醒”中删除以前存储的数据。
我必须在这里使用 CURSOR 吗? (注意:如果查询返回行,总会有至少 2 行受到影响)。我试图声明一个存储行的变量,但后来我得到一个“ORA-01422:精确提取返回的行数超过请求的行数”错误...
提前致谢!
【问题讨论】:
-
如果
REMINDER_USERS是REMINDER的子表,那么应该有一个外键将两个表绑定在一起。如果外键有ON DELETE CASCADE子句,那么REMINDER_USERS表会在REMINDER被删除时自动删除。 -
我不想使用
ON DELETE CASCADE,因为允许用户在REMINDER表中执行删除。如果他们这样做了,并且REMINDER_USERS表中有一行(指向不同的用户)不应被删除,则阻止删除。
标签: oracle plsql cursor parent-child