Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 help with update statement

Author  Topic 

tpiazza55
Posting Yak Master

162 Posts

Posted - 2012-01-06 : 13:15:37
i have a table that has the following columns

transid key value

so data exist like

transid key value
1 key1 value1
1 key2 value2
1 key3 value3


how would i update values for 2 different key in one update statement


X002548
Not Just a Number

15586 Posts

Posted - 2012-01-06 : 13:18:49
I don't update keys...but I'm pretty sure you don't mean that

What for example do you want to update and what should it look like AFTER the update?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

tpiazza55
Posting Yak Master

162 Posts

Posted - 2012-01-06 : 13:21:32
before

transid key value
1 key1 value1
1 key2 value2
1 key3 value3

after

transid key value
1 key1 value1_new
1 key2 value2
1 key3 value3_new
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-06 : 13:24:28
Well where do the new values COME from?

UPDATE t
SET value = xxx.value
FROM yourTable t
JOIN (
SELECT 'key1' AS Key, 'value1_new' AS value UNION ALL
SELECT 'key3' AS Key, 'value3_new' AS value) AS XXX
ON XXX.Key = t.Key

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

tpiazza55
Posting Yak Master

162 Posts

Posted - 2012-01-06 : 13:24:29
can you do something with a case statement and update the value twice in one update?

like

SET Value =

CASE
WHEN key = 'key1' THEN 'value1_new'
END,

Value =

CASE
WHEN key = 'key3' THEN 'value3_new'
END,
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-06 : 13:26:08
OK...where are you getting the data from? Excel? a file? another table?


Where?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

tpiazza55
Posting Yak Master

162 Posts

Posted - 2012-01-06 : 13:27:10
the new values come from a join on anther table
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-06 : 13:28:44
UPDATE t
SET value = xxx.value
FROM yourTable t
JOIN OtherTable xxx
ON XXX.Key = t.Key

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -