dbTalk Databases Forums  

UPDATE rows

comp.databases comp.databases


Discuss UPDATE rows in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
metaperl
 
Posts: n/a

Default UPDATE rows - 08-07-2007 , 06:24 AM






let's say I have a mapping table with columns A', B' and C'

and let's say I have a data table with columns, A, B, C, D, E, F

Now, what I want to do is update the data table with C' if A=A' and
B=B'

How can I do that? I am using MS-SQL 2000, but this must be a fairly
generic query.


Reply With Quote
  #2  
Old   
Tonkuma
 
Posts: n/a

Default Re: UPDATE rows - 08-07-2007 , 02:07 PM






I feel that your descriptions have some unusual notation and vague
requirements.
1) Usually, column name can't contain quotation mark(').
I think that it would be better to say(for example) column Am, Bm
and Cm(or simply A, B and C) instead of A', B' and C'.
2) You wrote "update the data table with C'". But, you didn't say what
column(s) of data table and how.
(Perhaps you want replace the value of column C with C'. But, you
didn't wrote that explicitly, it is only my guess. Your requirement
may be completely different.)
3) How to join rows?
Because you wrote "if A=A' and B=B'", while A and A' are columns of
different tables.
So, in my opinion, it is neccesary to define corresponding rows of
mapping table and data table.
(One popular and simple way is by join conditions.)

Considering those issues, I used my imagination as possible as I can,
then rewrited your requirement as followings.
Quote:
let's say I have a mapping table with columns A, B and C
and let's say I have a data table with columns, A, B, C, D, E, F
Now, what I want to do is to replace the value of C of data table(d)
with value of C in mapping table(m) which is correspomding row with d.A=m.A and d.B=m.B.
Assuming these boldly and too imaginary assumption would be right,
following may be an answer.
(If some of these asuumptions are not match with your requirement, I'm
sorry.
I hope you pointed out my misunderstandings and state more clearly
with your word.)
UPDATE data_table d
SET C = COALESCE((SELECT m.C
FROM mapping_table m
WHERE m.A=d.A and m.B=d.B
)
,d.C
);

or

MERGE INTO data_table d
USING mapping_table m
ON d.A=m.A and d.B=m.B
WHEN MATCHED THEN
UPDATE SET C = m.C;

(I don't know so much about MS-SQL Server 2000. So, it may be
necessary some modifications.)



Reply With Quote
  #3  
Old   
metaperl
 
Posts: n/a

Default Re: UPDATE rows - 08-08-2007 , 05:24 PM



On Aug 7, 3:07 pm, Tonkuma <tonk... (AT) fiberbit (DOT) net> wrote:

Quote:
Considering those issues, I used my imagination as possible as I can,
then rewrited your requirement as followings.

let's say I have a mapping table with columns A, B and C
and let's say I have a data table with columns, A, B, C, D, E, F
Now, what I want to do is to replace the value of C of data table(d)
with value of C in mapping table(m) which is correspomding row with d.A=m.A and d.B=m.B.

Yes, that is what I meant.
Quote:
UPDATE data_table d
SET C = COALESCE((SELECT m.C
FROM mapping_table m
WHERE m.A=d.A and m.B=d.B
)
,d.C
);

or

MERGE INTO data_table d
USING mapping_table m
ON d.A=m.A and d.B=m.B
WHEN MATCHED THEN
UPDATE SET C = m.C;

Thank you very much.

-- Terrence




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.