How to write a muliple update statements to a sigle table with several conditions?

Last Post 03 Jun 2008 06:36 AM by werstatyw. 3 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
werstatyw
New Member
New Member

--
03 Jun 2008 05:34 AM
Hello gurus.
I wonder what is the way to make this construction:
update <table>
set <column> = '<value>'
where <condition>

I have a several such constructions, could i make them in one?
Alexandr.
werstatyw
New Member
New Member

--
03 Jun 2008 06:36 AM
Currently i have several constructions:

update <table>
set <column> = '<value1>'
where <condition1>

update <table>
set <column> = '<value2>'
where <condition2>

Is it possible to union it in one statement?
nosepicker
New Member
New Member

--
03 Jun 2008 03:27 PM
If you're trying to update the same column, but with different values based on different conditions, you can use a CASE statement for that. If the conditions are really complex, it might be easier to just use separate UPDATE statements though. If you're trying to update different columns with different values based on different conditions, you have to use separate UPDATE statements. For the same column, you can do something like this:

UPDATE YourTable
SET YourColumn =
CASE WHEN SomeColumn = value1 THEN NewValue1
WHEN SomeColumn = value2 THEN NewValue2
END
werstatyw
New Member
New Member

--
03 Jun 2008 10:38 PM
Great! Thank you so much!
You are not authorized to post a reply.

Acceptable Use Policy