0

Is it possible to delete all records from table A which exist in table B if there are no primary or foreign keys? Similar to this answer:

DELETE a 
FROM @A a
WHERE EXISTS (SELECT a.* INTERSECT SELECT * FROM @B)

In DB2 each SELECT has to have a FROM clause and FROM sysibm.sysdummy1 does not work here.

Peter
  • 400
  • 1
  • 13

2 Answers2

1

Personally, I'd probably just use a multi-step process...

create table tmp as (
select * from @A
EXCEPT
select * from @B
);

delete from @A;

insert into @A
 select * from tmp;

Anything else I can think of seems to require an explicit list of column names.

delete 
from @A a
where exists (select *
              from @B b
              where a.Fld1 = b.Fld1
                   and a.Fld2 = b.Fld2
                  <...>
             );

Also considered a quantified predicate and the IN predicate...something like

delete 
from @A a
where (a.*) in (select * from @B);

delete 
from @A a
where (a.*) <> ALL (select * from @B);

But I don't believe (a.*) is considered a row-value-expression and regardless the documentation for both say that

SELECT * is not allowed in the outermost select lists of the fullselect

Lastly, note that all of these are going to have problems if there are any NULL columns.

Charles
  • 21,637
  • 1
  • 20
  • 44
0

try somthing like this :

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)   
)
Esperento57
  • 16,521
  • 3
  • 39
  • 45