Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update multiple columns in multiple rows


Update multiple columns in multiple rows

Author
Message
gj.thorpe
gj.thorpe
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 15
I have a table where I need to update multiple columns in each record (multiple rows) to match a single record already in the table.

For example:


Col 1 Col 2 Col 3 Col 4
10000 20.00 1 0
10001 0 0 1
10002 15.00 1 0
10003 0 0 0


So, for the above I would like to update values in Col 2, Col 3, Col 4 for records in (10001, 10002, 10003) to match values in Col 2, Col 3, Col 4 where Col 1 = 10000

I would like the data to end up like:


Col 1 Col 2 Col 3 Col 4
10000 20.00 1 0
10001 20.00 1 0
10002 20.00 1 0
10003 20.00 1 0



Is it possible to do this in a single statement?
ryan.mcatee
ryan.mcatee
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 134
I think you want something like this?

UPDATE {table}
SET [Col 2]=(SELECT [Col 2] FROM {table} WHERE [Col 1]=10000)
, [Col 3]=(SELECT [Col 3] FROM {table} WHERE [Col 1]=10000)
, [Col 4]=(SELECT [Col 4] FROM {table} WHERE [Col 1]=10000)




Edit: Misread this. And I am assuming [Col 1] is unique.
David Burrows
David Burrows
SSCertifiable
SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)

Group: General Forum Members
Points: 7963 Visits: 9411
ryan.mcatee (3/8/2013)
I think you want something like this?


or


UPDATE a
SET a.[Col 2] = b.[Col 2],
a.[Col 3] = b.[Col 3],
a.[Col 4] = b.[Col 4]
JOIN [table] b ON b.[Col 1] = 10000
WHERE a.[Col 1] > 10000



or


DECLARE @Col2 decimal,@Col3 int,@Col4 int
SELECT @Col2 = [Col 2],@Col3 = [Col 3],@Col4 = [Col 4]
FROM [table]
WHERE [Col 1] = 10000
UPDATE [table]
SET [Col 2] = @Col2,
[Col 3] = @Col3,
[Col 4] = @Col4
WHERE [Col 1] > 10000




Far away is close at hand in the images of elsewhere.

Anon.


gj.thorpe
gj.thorpe
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 15
David,

Thanks very much.

DECLARE @Col2 decimal,@Col3 int,@Col4 int
SELECT @Col2 = [Col 2],@Col3 = [Col 3],@Col4 = [Col 4]
FROM [table]
WHERE [Col 1] = 10000
UPDATE [table]
SET [Col 2] = @Col2,
[Col 3] = @Col3,
[Col 4] = @Col4
WHERE [Col 1] > 10000



Did exactly what was required.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search