If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > how to delete?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-17-10, 12:37
MarkhamDBA MarkhamDBA is offline
Registered User
 
Join Date: Dec 2008
Location: Toronto, Canada
Posts: 381
how to delete?

My sql is rusty I have a statement which selects a number of records from 3 tables:

select T1.col1,T1.col2,T2.col1,T3.col1
from table1 T1 ,table2 T2 ,table3 T3
where T1.col1=T2.col1
and T1.col2=T3.col1

How do I modify this statement to delete those records from table1? table1 does have PKs.
Thanks in advance
__________________
DB2 v9.5 ESE on AIX v6.1/ v9./10 on z/OS
Reply With Quote
  #2 (permalink)  
Old 08-17-10, 14:37
db2girl db2girl is offline
∞∞∞∞∞∞
 
Join Date: Aug 2008
Location: Toronto, Canada
Posts: 1,816
How about this:
delete from t1 where exists (select 1 from T1 , T2 ,T3 where T1.col1=T2.col1 and T1.col2=T3.col1)
Reply With Quote
  #3 (permalink)  
Old 08-17-10, 14:58
db2girl db2girl is offline
∞∞∞∞∞∞
 
Join Date: Aug 2008
Location: Toronto, Canada
Posts: 1,816
I tried with some data and it deleted all records from t1 - not good
Reply With Quote
  #4 (permalink)  
Old 08-17-10, 15:04
MarkhamDBA MarkhamDBA is offline
Registered User
 
Join Date: Dec 2008
Location: Toronto, Canada
Posts: 381
Quote:
Originally Posted by db2girl View Post
I tried with some data and it deleted all records from t1 - not good
yeah, i tried it too and deleted all records even if I had fetch first 3 rows only in the inner query.
__________________
DB2 v9.5 ESE on AIX v6.1/ v9./10 on z/OS
Reply With Quote
  #5 (permalink)  
Old 08-17-10, 15:38
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
delete from t1 where exists (select 1 from T1 , T2 ,T3 where T1.col1=T2.col1 and T1.col2=T3.col1)
Remove T1 from exists subselect, like:
Code:
DELETE FROM table1 T1
 WHERE EXISTS
       (SELECT 0
          FROM table2 T2 , table3 T3
         WHERE T1.col1 = T2.col1
           AND T1.col2 = T3.col1
       )
;
Reply With Quote
  #6 (permalink)  
Old 08-18-10, 10:01
MarkhamDBA MarkhamDBA is offline
Registered User
 
Join Date: Dec 2008
Location: Toronto, Canada
Posts: 381
perfect. Thank you very much, Tonkuma.

i could not find any useful (for my case) examples with EXISTS in any book (db2 documentation, db2 certification books) for some reason.
__________________
DB2 v9.5 ESE on AIX v6.1/ v9./10 on z/OS
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On