SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

vinylpimp
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')

then

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


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

Many thanks!

MuMu88
Aged Yak Warrior

547 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 = 
      CASE   
      WHEN ListingId in ('12345678')THEN '0'
      WHEN ListingId in ('87654321')THEN '1'
      ELSE
	  Price
END
WHERE ListingId in ('12345678', '87654321');


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

webfred
Flowing Fount of Yak Knowledge

Germany
8765 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

3698 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

vinylpimp
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  
 New 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.05 seconds. Powered By: Snitz Forums 2000