Solved

How do I do a multiple UPDATE ... WHERE ... ?

Posted on 2007-11-20
3
180 Views
Last Modified: 2010-04-21
Question:

Is there a more efficient way to write this simple UPDATE query:

update Table1
set Field1 = 'String1' where Field2 = 'Value1';
update Table1
set Field1 = 'String2' where Field2 = 'Value2';
...
update Table1
set Field1 = 'StringN' where Field2 = 'ValueN'; ?

0
Comment
Question by:masbe
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 20320785
not "simple", but single statement is possible:

update Table1
set Field1 = case field2
  when 'Value1' then 'String1' 
  when 'Value2' then 'String2' 
  ...
  when 'ValueN' then 'StringN' 
end
where Field2 IN ( 'Value1', 'Value2', ... 'ValueN' )
;

Open in new window

0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20320793
You have to write multiple statements for this. No other way round.
or if the where clause and value to be set have something common then do like this
update Table1
set Field1 = Replace(Field2, 'Value', 'String')
0
 

Author Closing Comment

by:masbe
ID: 31410144
Worked fine. Thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question