Solved

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

Posted on 2007-11-20
3
181 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 143

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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