Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
deanglen
Yak Posting Veteran
65 Posts |
Posted - 2013-05-15 : 04:55:24
|
HiI can update single records fine. But How do I update multiple rows in a table?ExampleI have a table called Customer. In that table there are two Rows. CustomerID and CustomerIDLevelI have a list of 300 CustomerID's that I want to replace from the current value of 0 to 3. Do they need to go in a comma delimited row? Any examples would be appreciated.Thanks- |
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2013-05-15 : 04:58:59
|
IS this you need?Update table_name set current_value=3 where CustomerID in(select CustomerID from table_name where <some condition> )Senthil Kumar C------------------------------------------------------MCITP - Database Administration SQL SERVER 2008MCTS - Database Development SQL SERVER 2008 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-15 : 05:02:10
|
there are two approaches1. use a condition using IN operator in WHERE and pass 300 ids in csv formatieWHERE CustomerID IN (ID1,ID2,...,ID300)2. Populate a table variable with 300 IDs and do a join with itDECLARE @CustomerIDs table(CustomerID int)INSERT @CustomerIDsSELECT ID1 UNION ALLSELECT ID2UNION ALLSELECT ID3...UNION ALLSELECT ID300SELECT t.*FROM YourTable tJOIn @CustomerIDs cON c.CustomerID = t.CustomerID ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
deanglen
Yak Posting Veteran
65 Posts |
Posted - 2013-05-15 : 05:04:48
|
I think so but what I want to do is to effectively update 300 records at once. So something like this was my original thoughtUPDATE CustomerSET CustomerIDLevel='3'WHERE CustomerID='1,2,3,4,5,6,7,8,9,10';But is it possible to have the Customer ID's in that format or do they need to be split somehow? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-15 : 05:09:42
|
quote: Originally posted by deanglen I think so but what I want to do is to effectively update 300 records at once. So something like this was my original thoughtUPDATE CustomerSET CustomerIDLevel='3'WHERE CustomerID='1,2,3,4,5,6,7,8,9,10';But is it possible to have the Customer ID's in that format or do they need to be split somehow?
see my suggestionyou need to use IN and then pass the comma separated values------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
deanglen
Yak Posting Veteran
65 Posts |
Posted - 2013-05-15 : 05:28:45
|
First one worked fine. Thanks! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-15 : 05:34:16
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|