我正在使用SQL Server 2012。
我想更新表格中的一行,使其等于同一表格中除主键字段之外的另一行的值,请参阅下面的示例。这样做的最佳方式是什么?
Primary Key Field One Field Two Field Three
ABS 5 6 2
NJK 3 2 3
因此更新查询应该返回一个看起来像
的结果 Primary Key Field One Field Two Field Three
ABS 5 6 2
NJK 5 6 2
答案 0 :(得分:1)
UPDATE A
SET A.[Field One] = B.[Field One],
A.[Field Two] = B.[Field Two],
A.[Field Three] = B.[Field Three]
FROM [table] A
CROSS JOIN (SELECT * FROM [table] WHERE [Primary Key] = 'ABS') B
答案 1 :(得分:1)
UPDATE x
SET
x.[Field One] = y.[Field One],
x.[Field Two] = y.[Field Two],
x.[Field Three] = y.[Field Three]
FROM
<tablename> x
JOIN
<tablename> y
ON
x.[Primary Key] = 'NJK' AND
y.[Primary Key] = 'ABS'