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

Deleting records from one table with multiple conditions


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

New User


Joined: 17 Sep 2008
Posts: 75
Location: bangalore

PostPosted: Fri Nov 07, 2008 2:50 pm
Reply with quote

Hi Friends,

It would be really great if some body help me to resolve the following problem.

I have two tables as follows
Table A
Code:
ColA1,ColA2,ColA3,COLA4,COLA5

Table B
Code:
ColB1,ColB2,ColB3,COLB4,COLB5 ...till COLB10

i need to write a delete query which should delete the records from TableA with the following conditions.
Code:
ColA1 = ColB1
ColA2 = ColB2
ColA3 = ColB3

I tried to use INNER JOIN but failed and got -199 SQLCODE for JOIN keyword.


Thanks & Regards,
Mallik
Back to top
View user's profile Send private message
anandinmainframe

Active User


Joined: 31 May 2007
Posts: 171
Location: India

PostPosted: Fri Nov 07, 2008 3:20 pm
Reply with quote

Hi,
DELETE FROM Table A
where ColA1 = ColB1
and ColA2 = ColB2
and ColA3 = ColB3
will do a favour
Back to top
View user's profile Send private message
mallik4u

New User


Joined: 17 Sep 2008
Posts: 75
Location: bangalore

PostPosted: Fri Nov 07, 2008 3:32 pm
Reply with quote

Hi,

Thanks for the reply!!
But I have a question
How will you get ColB1, ColB2 and ColB3?


Regards,
Mallik
Back to top
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Fri Nov 07, 2008 3:40 pm
Reply with quote

[quote=malik]
I tried to use INNER JOIN but failed and got -199 SQLCODE for JOIN keyword.
[/quote]
Can you post your join query?

[quote=anand]
DELETE FROM Table A
where ColA1 = ColB1
and ColA2 = ColB2
and ColA3 = ColB3
will do a favour
[/quote]
will not work ColB1 ColB2 ColB3 are not actual values (literal here i mean)
It should be matched using join
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Fri Nov 07, 2008 3:41 pm
Reply with quote

Anand,

The OP is trying to join Table A and Table B and it's not a single table.
Back to top
View user's profile Send private message
mallik4u

New User


Joined: 17 Sep 2008
Posts: 75
Location: bangalore

PostPosted: Fri Nov 07, 2008 3:46 pm
Reply with quote

Hi,

I have got the answer to my query from following IBM site

publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db2.doc.apsg/bjnqsubq1002042.htm
Following query wii work
Code:
DELETE FROM TableA
  WHERE (ColA1, ColA2,COlA3) IN (SELECT ColB1, ColB2,COlB3
                               FROM TableB);

Guys thanks for the help!!!

Edited: Please use BBcode when You post some code, that's rather readable...Anuj
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Fri Nov 07, 2008 3:52 pm
Reply with quote

Mallik,

Thanks for posting the solution. icon_biggrin.gif
Back to top
View user's profile Send private message
anandinmainframe

Active User


Joined: 31 May 2007
Posts: 171
Location: India

PostPosted: Fri Nov 07, 2008 4:07 pm
Reply with quote

sorry i have forgetten that.
Back to top
View user's profile Send private message
r2k1984

New User


Joined: 21 Jun 2005
Posts: 69
Location: chennai

PostPosted: Tue Nov 11, 2008 11:36 am
Reply with quote

The above query can be modified in to

DELETE FROM TableA ,TableB
WHERE (ColA1= ColB1,
ColA2=ColB2,
ColA3=COlB3);
Back to top
View user's profile Send private message
Bharath Bhat

Active User


Joined: 20 Mar 2008
Posts: 283
Location: chennai

PostPosted: Tue Nov 11, 2008 11:41 am
Reply with quote

r2k1984 wrote:
The above query can be modified in to

DELETE FROM TableA ,TableB
WHERE (ColA1= ColB1,
ColA2=ColB2,
ColA3=COlB3);


This query does not work. That is the conclusion of this discussion.

The query which works has already been posted.
Back to top
View user's profile Send private message
r2k1984

New User


Joined: 21 Jun 2005
Posts: 69
Location: chennai

PostPosted: Tue Nov 11, 2008 12:01 pm
Reply with quote

Small modification to the query

DELETE FROM TableA ,TableB
WHERE (TABLEA.ColA1= TABLEA.ColB1,
TABLEA.ColA2=TABLEB.ColB2,
TABLEA.ColA3=TABLEB.COlB3);

This will surely works
Back to top
View user's profile Send private message
Bharath Bhat

Active User


Joined: 20 Mar 2008
Posts: 283
Location: chennai

PostPosted: Tue Nov 11, 2008 12:53 pm
Reply with quote

Did you test this query?
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 Compare 2 files(F1 & F2) and writ... JCL & VSAM 8
No new posts INCLUDE OMIT COND for Multiple values... DFSORT/ICETOOL 5
No new posts Load new table with Old unload - DB2 DB2 6
No new posts Compare only first records of the fil... SYNCSORT 7
No new posts Pulling a fixed number of records fro... DB2 2
Search our Forums:

Back to Top