我在SQL Server存储过程中有一个带有Id和日期列的临时表表。该表已经填充了日期,我希望能够根据日期的升序在id列中添加序列号
我试过以下
UPDATE #Weeks
SET Id = (
SELECT ROW_NUMBER() OVER (ORDER BY WeekDate ASC) AS Id
FROM #Weeks AS DATA);
并收到此错误。
子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。
感谢任何帮助。
这对我有用.....
更新#Weeks SET Id = tab.Id 来自#Weeks w 内部联接 ( 选择WeekDate, ROW_NUMBER()OVER(由WeekDate ASC订购)AS Id 来自#Weeks)Tab ON w.WeekDate = Tab.WeekDate
答案 0 :(得分:1)
试试这个:
UPDATE #Weeks
SET Id = Tab.Id
FROM #Week w
INNER JOIN (
SELECT Id,
ROW_NUMBER() OVER (ORDER BY WeekDate ASC) AS Id
FROM #Weeks ) Tab ON w.Id = Tab.Id
由于子查询返回多行,而=运算符只返回一行。
答案 1 :(得分:0)
试试这个
;with cte as
(
SELECT Id,ROW_NUMBER() OVER (ORDER BY WeekDate ASC) AS DATA FROM #Weeks
)
UPDATE #Weeks SET Id=DATA from cte
答案 2 :(得分:0)
UPDATE #Weeks
SET Id = t.Id1
FROM #Week w
INNER JOIN
(
SELECT Id, ROW_NUMBER() OVER (ORDER BY WeekDate ASC) AS Id1
FROM #Weeks ) T ON w.Id = t.Id