Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-20
3
Medium Priority
?
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

715 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