UPDATE question => updating with information from other table [message #434435] |
Wed, 09 December 2009 05:02 |
s-t-e-i-n
Messages: 4 Registered: December 2009
|
Junior Member |
|
|
I used the search function, but I´m not sure what to ask.
Here is my task:
I have 2 Tables:
T1 (NR, CURRENTCONTENT)
T2 (NR, NEWCONTENT)
I want to do something like this: (obviously no valid statement)
UPDATE T1
SET T1.CURRENTCONTENT = T2.NEWCONTENT
The tables can by joined on T1.NR = T2.NR
I have no Idea how to join within an UPDATE statement...
|
|
|
|
|
Re: UPDATE question => updating with information from other table [message #434473 is a reply to message #434442] |
Wed, 09 December 2009 06:11 |
s-t-e-i-n
Messages: 4 Registered: December 2009
|
Junior Member |
|
|
JRowbottom wrote on Wed, 09 December 2009 05:16Traditional Approach
UPDATE t1
set currentcontent = (SELECT newcontent
FROM t2
where t1.nr = t2.nr)
Thanks for the hint. But this will only work if there is a row in t2 for every t1.NR. Else you update the CURRENTVALUE WITH NULL, whis is ugly when it is (part of) a key.
When t1 has 100 rows, and t2 has 20, and I want to update CURRENTCONTENT with the 20 matching NEWCONTENT, what do I do?
|
|
|
|
|
|
|
Re: UPDATE question => updating with information from other table [message #434510 is a reply to message #434478] |
Wed, 09 December 2009 08:20 |
s-t-e-i-n
Messages: 4 Registered: December 2009
|
Junior Member |
|
|
Michel Cadot wrote on Wed, 09 December 2009 06:19So add a where clause to your update, like "where exists ..."
Regards
Michel
Where would I place the WHERE EXISTS ? And what would be in the brackets? I would have to use "WHERE EXISTS(NEWCONTENT)", but this won´t work because there is no way to link to the NEWCONTENT and no alias, or am I missing something?
T1: 100 rows
T2: 20 rows
UPDATE T1
SET CURRENTCONTENT = (SELECT NEWCONTENT
FROM T2
WHERE T1.NR = T2.NR)
WHERE EXISTS (??)
|
|
|
|
|
Re: UPDATE question => updating with information from other table [message #434524 is a reply to message #434516] |
Wed, 09 December 2009 08:59 |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
It's the lowest non-zero natural number, and the identity value for multiplication and division operations
An EXISTS subquery just checks to see if there are any rows to return - by not selecting a row from a table you allow the CBO to potentially pick more index only access paths and run the subquery quicker
|
|
|
|