MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help needed on creating Update query

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
anbesivam
Beginner


Joined: 09 Aug 2006
Posts: 66
Topics: 14

PostPosted: Fri Jul 15, 2011 11:33 am    Post subject: Help needed on creating Update query Reply with quote

Hi,

I am getting 1000 records from 4 tables and I need to update perticular column in the 4th table.

For example, I am getting results from Table A, B, C & D using Joins and based on conditions.

Code:

Col 1 (Tab A)   Col2 (Tab B)   Col 3 (Tab 3)     Col 4 (Tab 4)
---------------   --------------   -----------------   ----------------
xxxxx                    111                  2222         15.07.2011   
YYYYY                    111                  2222         15.07.2011   


In this, I need to update only the Col 4 (Table 4) to 18.07.2011.

I have tried some combination with Update statement. But it is not working out.. Please guide me.

Thank you very much for your time.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Jul 15, 2011 12:20 pm    Post subject: Reply with quote

anbesivam,

Show us the select statement of joining the 4 tables and how you are getting the results. Do you need the 4 columns or your primary goal is to update the COL4 ?

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
CZerfas
Intermediate


Joined: 31 Jan 2003
Posts: 211
Topics: 8

PostPosted: Mon Jul 18, 2011 3:05 am    Post subject: Reply with quote

The main problem with the update statement is, that you don't have a FROM clause where you could list more than one table.

That means, that in case of the need of accessing more than one table, you have to put these into subselects (EXIST queries) within the WHERE clause of your UPDATE statement. That doesn't make your statement more readable but gives you the desired results.

regards
Christian
Back to top
View user's profile Send private message
GuyC
Supermod


Joined: 27 May 2011
Posts: 35
Topics: 0

PostPosted: Mon Jul 18, 2011 5:11 am    Post subject: Reply with quote

in other words, use a correlated subquery:
Code:
update tab1 A set (col4,col5,col6) =
(select b.col4,b.col5,b.col6 from anyjoin B where A.pk = b.pk)
where exists
(select b.col4,b.col5,b.col6 from anyjoin B where a.pk = b.pk)
Back to top
View user's profile Send private message
anbesivam
Beginner


Joined: 09 Aug 2006
Posts: 66
Topics: 14

PostPosted: Tue Jul 19, 2011 5:43 am    Post subject: Reply with quote

Thank you very much all of you for the detailed useful reply.

As the update query getting really complecated, we added some selection criteria to reduce the 4th table's results to less than 50 records and manually updated the dates.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group