SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


update record from another record in same table


update record from another record in same table

Author
Message
Marcus Farrugia
Marcus Farrugia
Old Hand
Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)

Group: General Forum Members
Points: 397 Visits: 505
Hi, I'm looking for the best way to update pkid 1 = pkid 2

update testTable
set
a.fname = b.fname
from
(select * from testTable where pkid = 1) as a
join
(select * from testTable where pkid = 2) as b
Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40594 Visits: 38567
Marcus Farrugia (5/1/2013)
Hi, I'm looking for the best way to update pkid 1 = pkid 2

update testTable
set
a.fname = b.fname
from
(select * from testTable where pkid = 1) as a
join
(select * from testTable where pkid = 2) as b



This:



update dbo.testTable set
fname = (select fname from dbo.testTable where pkid = 2)
where
pkid = 1;




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Marcus Farrugia
Marcus Farrugia
Old Hand
Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)

Group: General Forum Members
Points: 397 Visits: 505


update dbo.testTable set
fname = (select fname from dbo.testTable where pkid = 2)
where
pkid = 1;





Thanks Lynne!
if there were multiple columns would I just do the same for each column, ie:

update dbo.testTable set
fname = (select fname from dbo.testTable where pkid = 2),
lname = (select fname from dbo.testTable where pkid = 2),
address = (select address from dbo.testTable where pkid = 2
where
pkid = 1;
OTF
OTF
Mr or Mrs. 500
Mr or Mrs. 500 (510 reputation)Mr or Mrs. 500 (510 reputation)Mr or Mrs. 500 (510 reputation)Mr or Mrs. 500 (510 reputation)Mr or Mrs. 500 (510 reputation)Mr or Mrs. 500 (510 reputation)Mr or Mrs. 500 (510 reputation)Mr or Mrs. 500 (510 reputation)

Group: General Forum Members
Points: 510 Visits: 4128
Marcus Farrugia (5/1/2013)


update dbo.testTable set
fname = (select fname from dbo.testTable where pkid = 2)
where
pkid = 1;





Thanks Lynne!
if there were multiple columns would I just do the same for each column, ie:

update dbo.testTable set
fname = (select fname from dbo.testTable where pkid = 2),
lname = (select fname from dbo.testTable where pkid = 2),
address = (select address from dbo.testTable where pkid = 2
where
pkid = 1;


Maybe you could try something along these lines if you have multiple columns:


UPDATE tt1
SET FName = tt2.FName,
LName = tt2.LName,
[Address] = tt2.[Address]

FROM dbo.TestTable tt1 JOIN
(SELECT 1 as pkid, FName, LName, [Address] FROM dbo.TestTable WHERE pkid=2) tt2
ON tt1.pkid = tt2.pkid
WHERE tt1.pkid=1



Please note it is untested as you have supplied no test data or ddl.
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