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
 Updating multiple rows
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

deanglen
Yak Posting Veteran

64 Posts

Posted - 05/15/2013 :  04:55:24  Show Profile  Reply with Quote
Hi

I can update single records fine. But How do I update multiple rows in a table?

Example

I have a table called Customer. In that table there are two Rows. CustomerID and CustomerIDLevel

I 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
Flowing Fount of Yak Knowledge

India
1007 Posts

Posted - 05/15/2013 :  04:58:59  Show Profile  Visit senthil_nagore's Homepage  Send senthil_nagore a Yahoo! Message  Reply with Quote
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 2008
MCTS - Database Development SQL SERVER 2008
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 05/15/2013 :  05:02:10  Show Profile  Reply with Quote
there are two approaches

1. use a condition using IN operator in WHERE and pass 300 ids in csv format

ie

WHERE CustomerID IN (ID1,ID2,...,ID300)

2. Populate a table variable with 300 IDs and do a join with it

DECLARE @CustomerIDs table
(
CustomerID int
)
INSERT @CustomerIDs
SELECT ID1 
UNION ALL
SELECT ID2
UNION ALL
SELECT ID3
...
UNION ALL
SELECT ID300


SELECT t.*
FROM YourTable t
JOIn @CustomerIDs c
ON c.CustomerID = t.CustomerID


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

deanglen
Yak Posting Veteran

64 Posts

Posted - 05/15/2013 :  05:04:48  Show Profile  Reply with Quote
I think so but what I want to do is to effectively update 300 records at once. So something like this was my original thought

UPDATE Customer
SET 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?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 05/15/2013 :  05:09:42  Show Profile  Reply with Quote
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 thought

UPDATE Customer
SET 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 suggestion
you need to use IN and then pass the comma separated values

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

deanglen
Yak Posting Veteran

64 Posts

Posted - 05/15/2013 :  05:28:45  Show Profile  Reply with Quote
First one worked fine. Thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 05/15/2013 :  05:34:16  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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.08 seconds. Powered By: Snitz Forums 2000