IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

DB2 DELETE - multiple Tables.


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
DB2 Guy

New User


Joined: 28 Oct 2008
Posts: 98
Location: Cubicle

PostPosted: Mon Jun 04, 2012 5:47 pm
Reply with quote

Hi,

Question is - what are the ways to delete the rows from two DB2 Tables which don't have any referential integrity, so, in a way, mutaully exclusive but related to each other as follows:
Code:
SELECT  * FROM          M.table1 M1 ,           M.table2M2
WHERE M1.col_x = '1234' AND M1.col_y =  'abcd'           
AND M1.col1 = 'pgm'                             
AND M1.date1 > '2012-01-26'                                 
AND M1.col2 <>'  '                                   
AND M1.col3 = M2.col3
AND M1.col4 = M2.col4
AND M1.col5 = M2.col5
AND M1.col6 = M2.col6
AND M1.col7 = M2.col7
AND M1.col8 = M2.col8
when I execute this query, I get around 300 rows -- which I want to delete.

One way is using two different DELETE queries on these two tables. The second method I thought of using "WHERE EXISTS" as shown below:
Code:
DELETE FROM M.table M1 WHERE EXISTS
(SELECT  * FROM          M.table1 M1 ,           M.table2M2
WHERE M1.col_x = '1234' AND M1.col_y =  'abcd'           
AND M1.col1 = 'pgm'                             
AND M1.date1 > '2012-01-26'                                 
AND M1.col2 <>'  '                                   
AND M1.col3 = M2.col3
AND M1.col4 = M2.col4
AND M1.col5 = M2.col5
AND M1.col6 = M2.col6
AND M1.col7 = M2.col7
AND M1.col8 = M2.col8)
but,
1. It tries to DELETE much more than 300 rows.
2. I still issue only one table delete by using "DELETE FROM M.table M1", so how can I delete the data from 2nd table too, using a single query?
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Mon Jun 04, 2012 6:33 pm
Reply with quote

without having db2 available referential restraints,
maybe a trigger?

your query would remain 1 query, just need another to do the work.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Tue Jun 05, 2012 1:42 pm
Reply with quote

The problem is you are using M1 twice :
this should delete all the requesrted rows out of M1
Rows in M2 will remain.
Code:
DELETE FROM M.table M1 WHERE
M1.col_x = '1234' AND M1.col_y =  'abcd'           
AND M1.col1 = 'pgm'                             
AND M1.date1 > '2012-01-26'                                 
AND M1.col2 <>'  '                                 
AND
EXISTS
(SELECT  * FROM    M.table2M2
WHERE    M1.col3 = M2.col3
AND M1.col4 = M2.col4
AND M1.col5 = M2.col5
AND M1.col6 = M2.col6
AND M1.col7 = M2.col7
AND M1.col8 = M2.col8)


Shame someone calling himself "DB2 Guy" doesn't know this
Back to top
View user's profile Send private message
PeterHolland

Global Moderator


Joined: 27 Oct 2009
Posts: 2481
Location: Netherlands, Amstelveen

PostPosted: Tue Jun 05, 2012 2:03 pm
Reply with quote

Quote:

Shame someone calling himself "DB2 Guy" doesn't know this


Don't be that harsh, his nick is not "DB2 Master". Guy means probably youngster.
Back to top
View user's profile Send private message
DB2 Guy

New User


Joined: 28 Oct 2008
Posts: 98
Location: Cubicle

PostPosted: Tue Jun 05, 2012 2:28 pm
Reply with quote

Thanks GuyC.

Even with that you end up writing two differnt queries if you need to delete the rows from M2? That's why I asked for:
Quote:
2. I still issue only one table delete by using "DELETE FROM M.table M1", so how can I delete the data from 2nd table too, using a single query?
I did try what you show but didn't put it here in original post of mine -- I agree, that's fault on my part.

Thank God I did not use "DB2 DBA" as my avtar-name here. But then what's there in the name, sqlcode(1) usally solves problem on (DF)SORT, no! Okay, jokes apart...icon_smile.gif.

As an application developer, you're rather supposed to maintain things and not develop them and that bites you back - I'm reading SQL Cook Book by Graeme Birchall and I think it has got anough SQLs I'd ever need as a programer (not DBA) but then I'm still a Guy, as Peter says! icon_smile.gif
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Tue Jun 05, 2012 6:12 pm
Reply with quote

You can only delete from 1 table in one query...
You can have DB2 do some extra stuff via Referential Integrity Cascade delete or via Triggers as dbz suggested.

you can delete from 1 table with one query, but then the data needed for knowing what to delete in the other table is probably gone.

If you need both tables deleted you'll probably end up with a program containing a cursor-loop with 2 deletes inside.

So in short, answering to your first question : YOU CAN'T.

PS.: to make sure, I even tried "delete from ... (select ... from old table (Delete ...) )" but as expected this doesn't work
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts INCLUDE OMIT COND for Multiple values... DFSORT/ICETOOL 5
No new posts DELETE SPUFI DB2 1
No new posts DSNTIAUL driven delete IBM Tools 0
No new posts Replace Multiple Field values to Othe... DFSORT/ICETOOL 12
No new posts Multiple table unload using INZUTILB DB2 2
Search our Forums:

Back to Top