|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, May 07, 2013 9:56 AM
Points: 2,
Visits: 9
|
|
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?
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, March 28, 2013 11:07 AM
Points: 43,
Visits: 118
|
|
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.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: 2 days ago @ 10:59 AM
Points: 6,350,
Visits: 5,355
|
|
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.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, May 07, 2013 9:56 AM
Points: 2,
Visits: 9
|
|
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.
|
|
|
|