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 

Update one table column with an another table column

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


Joined: 16 May 2004
Posts: 68
Topics: 26

PostPosted: Wed Jul 06, 2005 8:15 am    Post subject: Update one table column with an another table column Reply with quote

Hi

I have two tables with their columns as follows.

Code:
TA - TAC1, TAC2, TAC3, TAC4
TB - TBC1, TBC2, TBC3, TBC4


My requirement is to update TBC4 with TAC4 if
TAC1 = TBC1 AND
TAC2 = TBC2 AND
TAC3 = TBC3.

I have written one SPUFI as follows but not giving result.

Code:
UPDATE  TB
   SET  TBC4  =  (
SELECT  TAC4
  FROM  TA
 WHERE  TAC1 = TBC1
   AND  TAC2 = TBC2
   AND  TAC3 = TBC3  )  ;


Can anybody help me please....

Regards
Sridhar P
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: Wed Jul 06, 2005 8:36 am    Post subject: Reply with quote

psridhar,

Try this

Code:

UPDATE TB X                               
   SET TBC4 = (SELECT TAC4                 
                 FROM TA Y                 
                WHERE X.TBC1 = Y.TAC1     
                  AND X.TBC2 = Y.TAC2     
                  AND X.TBC3 = Y.TAC3)
;   


Hope this helps...

Cheers

kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
videlord
Beginner


Joined: 09 Dec 2004
Posts: 147
Topics: 19

PostPosted: Wed Jul 06, 2005 10:50 am    Post subject: Reply with quote

Is your requirement feasible?
If no unquie constraints on TBC1 TBC2 TBC3,
then how to set TBA4?
more than 1 possible value
Back to top
View user's profile Send private message
psridhar
Beginner


Joined: 16 May 2004
Posts: 68
Topics: 26

PostPosted: Wed Jul 06, 2005 11:40 pm    Post subject: Reply with quote

Hi Kolusu,

I tried your SQL and got the results. Thanks for the help.

I faced one problem while executing the SQL. I have some rows in TB which does not have corresponding rows in TA satisfying the WHERE. I got SQLCODE = -407. I deleted all such rows in TB and ran the SQL again and it gave correct results.

My understanding is, if a row in TB does not have corresponding row in TA then there should not be any updates to that row in TB. But why SQLCODE = -407.????? Can you please reply me if you have time.

Thanks and Regards
Sridhar P
Back to top
View user's profile Send private message
videlord
Beginner


Joined: 09 Dec 2004
Posts: 147
Topics: 19

PostPosted: Thu Jul 07, 2005 4:22 am    Post subject: Reply with quote

The SQL would update all records in TB.
Consider this scenario, for one record in TB, there is no record inTA match this record with the 3 column.
Then the SQL is same as
UPDATE TB set TBC4=NULL where TBC1=xx and TBC2=xx and TBC3=xx

if TBC4 is define with NOT NULL, SQLCODE -407 appeared.
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: Thu Jul 07, 2005 6:31 am    Post subject: Reply with quote

psridhar,


Videlord is right on about the column declaration. If the column is defined as NOT NULL then you would get a sqlcode of -407

Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Wed Sep 28, 2005 6:17 pm    Post subject: Reply with quote

This is how I get around SQLCODE -407:
Code:

UPDATE TB X                               
   SET TBC4 =
           (SELECT TAC4                 
                 FROM TA Y                 
                WHERE X.TBC1 = Y.TAC1     
                    AND X.TBC2 = Y.TAC2     
                    AND X.TBC3 = Y.TAC3)
 WHERE 0 NOT IN (  SELECT COUNT(*)
     FROM TA A                 
                WHERE X.TBC1 = A.TAC1     
                    AND X.TBC2 = A.TAC2     
                    AND X.TBC3 = A.TAC3)
;   

_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Thu Sep 29, 2005 12:10 am    Post subject: Reply with quote

Hi,

I have never written sql, probably so, i don't understand why kolusu's code worked and sridhar's didn't. Since column names are diffrenet in both the tables, does the x and y alias really matter.

thanks,
diba.
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


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

PostPosted: Thu Sep 29, 2005 4:45 am    Post subject: Reply with quote

Quote:

Since column names are diffrenet in both the tables, does the x and y alias really matter.



Dibakar,

Sridhar's query is trying to update every row of table A with the column value of table B. The query I posted will try to update when there is a match in both the tables A and B. I needed the Alias names for the join operation of the tables.

Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Sat Oct 01, 2005 1:34 am    Post subject: Reply with quote

Kolusu,

Thanks for your clarification but I still have doubt.

Suppose this is my input -

Code:

        TA                           TB           
                                                 
TAC1 TAC2 TAC3 TAC4          TBC1 TBC2 TBC3 TBC4 
 1    2    3    4             1    2    3    4     
 5    6    7    8             5    6    7    9     
 A    B    C    D             E    F    G    H     
 I    J    K    L             I    J    L    M     


Then I beleive this will be output of your SQL will be -

Code:

        TB         
                   
TBC1 TBC2 TBC3 TBC4
 1    2    3    4   
 5    6    7    8   
 E    F    G    H   
 I    J    L    M   


I am not clear what will be the output of Sridhar's code.

Thanks,
Diba.
Back to top
View user's profile Send private message Send e-mail
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