View previous topic :: View next topic |
Author |
Message |
psridhar Beginner
Joined: 16 May 2004 Posts: 68 Topics: 26
|
Posted: Wed Jul 06, 2005 8:15 am Post subject: Update one table column with an another table column |
|
|
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 |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Wed Jul 06, 2005 8:36 am Post subject: |
|
|
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 |
|
 |
videlord Beginner
Joined: 09 Dec 2004 Posts: 147 Topics: 19
|
Posted: Wed Jul 06, 2005 10:50 am Post subject: |
|
|
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 |
|
 |
psridhar Beginner
Joined: 16 May 2004 Posts: 68 Topics: 26
|
Posted: Wed Jul 06, 2005 11:40 pm Post subject: |
|
|
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 |
|
 |
videlord Beginner
Joined: 09 Dec 2004 Posts: 147 Topics: 19
|
Posted: Thu Jul 07, 2005 4:22 am Post subject: |
|
|
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 |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Thu Jul 07, 2005 6:31 am Post subject: |
|
|
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 |
|
 |
NASCAR9 Intermediate
Joined: 08 Oct 2004 Posts: 274 Topics: 52 Location: California
|
Posted: Wed Sep 28, 2005 6:17 pm Post subject: |
|
|
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 |
|
 |
Dibakar Advanced

Joined: 02 Dec 2002 Posts: 700 Topics: 63 Location: USA
|
Posted: Thu Sep 29, 2005 12:10 am Post subject: |
|
|
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 |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Thu Sep 29, 2005 4:45 am Post subject: |
|
|
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 |
|
 |
Dibakar Advanced

Joined: 02 Dec 2002 Posts: 700 Topics: 63 Location: USA
|
Posted: Sat Oct 01, 2005 1:34 am Post subject: |
|
|
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 |
|
 |
|
|