如果没有主键或外键,是否可以删除表B中存在的表A中的所有记录?与this answer类似:
DELETE a
FROM @A a
WHERE EXISTS (SELECT a.* INTERSECT SELECT * FROM @B)
在DB2中,每个SELECT都必须有一个FROM子句,而FROM sysibm.sysdummy1在这里不起作用。
答案 0 :(得分:1)
就个人而言,我可能只是使用一个多步骤的过程......
create table tmp as (
select * from @A
EXCEPT
select * from @B
);
delete from @A;
insert into @A
select * from tmp;
我能想到的任何其他内容似乎都需要明确的列名列表。
delete
from @A a
where exists (select *
from @B b
where a.Fld1 = b.Fld1
and a.Fld2 = b.Fld2
<...>
);
也被认为是量化的谓词和IN谓词......类似于
delete
from @A a
where (a.*) in (select * from @B);
delete
from @A a
where (a.*) <> ALL (select * from @B);
但我不相信(a。*)被认为是行值表达式,无论两者的文档都说
全选
的最外层选择列表中不允许使用SELECT *
最后,请注意,如果有任何 NULL 列,所有这些都会出现问题。
答案 1 :(得分:0)
尝试这样的事情:
delete from @A a
where exists
(
select * from @b b
where
(b.field1=a.field1 or b.field1 is null and a.field1 is null) and
(b.field2=a.field2 or b.field2 is null and a.field2 is null) and
(b.field3=a.field3 or b.field3 is null and a.field3 is null)
)