我有以下查询允许我按行号连接两个表,我正在尝试修改它,以便用随机名称表中的随机名称更新订阅者表。
select fo.*, so.*
from (select *,
row_number() over (order by FirstName) as rn
from randomnames
) fo
join (select *,
row_number() over (order by ID) as rn
from subscribers) so on fo.rn = so.rn
我将其更新为以下内容以使其正常工作
update subscribers set
FirstName = fo.Firstname,
LastName = fo.LastName
from
(select *,
row_number() over (order by firstname) as rn
from _randomnames
) fo
join (select *,
row_number() over (order by ID desc) as rn
from subscribers) so on fo.rn = so.rn
where so.ID = subscribers.ID
答案 0 :(得分:0)
尝试以下查询:
;WITH CTE AS
(
SELECT *, ROW_NUMBER() OVER (ORDER BY ID) AS rn
FROM subscribers
)
UPDATE so
SET so.[Name] = fo.[Name]
FROM CTE so
INNER JOIN (SELECT *, ROW_NUMBER() OVER (ORDER BY FirstName) AS rn
FROM randomnames) fo
ON fo.rn = so.rn