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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update multiple entries to different values
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

16 Posts

Posted - 04/30/2013 :  12:53:45  Show Profile  Reply with Quote
Currently I update one entry at a time

ListingId Price
12345678 0
87654321 1

Update [XXX].[dbo].[listing]
set Price = '0'
where ListingId in ('12345678')


Update [XXX].[dbo].[listing]
set Price = '1'
where ListingId in ('87654321')

Is there a way to do both executions with one command?

Many thanks!

Aged Yak Warrior

549 Posts

Posted - 04/30/2013 :  13:56:47  Show Profile  Reply with Quote
You may want to try something like this:

UPDATE [XXX].[dbo].[listing]
SET   Price = 
      WHEN ListingId in ('12345678')THEN '0'
      WHEN ListingId in ('87654321')THEN '1'
WHERE ListingId in ('12345678', '87654321');

Edited by - MuMu88 on 04/30/2013 13:59:38
Go to Top of Page

Flowing Fount of Yak Knowledge

8781 Posts

Posted - 04/30/2013 :  13:59:30  Show Profile  Visit webfred's Homepage  Reply with Quote
update listing
set Price = case when listingid = 12345678 then 0 else 1 end
where listingid in(12345678,87654321)

Too old to Rock'n'Roll too young to die.

Edited by - webfred on 04/30/2013 14:00:06
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 04/30/2013 :  14:15:13  Show Profile  Reply with Quote
To add to what MuMu suggested, the thought is that, if you had a criterion that tells you which ListingID's should get zero's and which should get ones, you can use those in the WHEN expressions. As an example (and I am making up this out of thin air), let us say you had a column called expiry_date. Also, let us assume that if expiry_date is earlier than now, you want to mark listing id as 0, otherwise set it to 1. Then, you would modify MuMu's WHEN expressions like this:
      WHEN expiry_date < getdate() THEN '0'
      WHEN expiry_date >= getdate() THEN '1'
Go to Top of Page

Starting Member

16 Posts

Posted - 04/30/2013 :  17:53:20  Show Profile  Reply with Quote
Thank you so much guys, all scenarios are pretty much covered!
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000