带ORDER BY的T-SQL Update语句

时间:2010-10-14 10:27:15

标签: sql-server sql-server-2005 tsql

我正在使用SQL Server 2005.我想更新Order BY MatchId订单等字段,如下面的查询。但是匹配的字段不是由MatchId订购的。

DECLARE @counter int

SET @counter = 10008

UPDATE Matches 
SET @counter = MatchNumberCounter = @counter + 1
WHERE MatchId IN 
      (SELECT TOP (232) MatchId FROM Matches WHERE LeagueStatueId = 280 AND Week <> 1 
       ORDER BY Week, MatchDate, MatchTime)

现在谢谢

1 个答案:

答案 0 :(得分:1)

看起来你正试图做一个“古怪的更新”。这是一种未记录的技术,需要满足许多条件才能实现。 These are discussed here(需要免费注册)

你应该可以做这样的事情。

;With cte As
(
SELECT TOP (232) MatchId ,
                 MatchNumberCounter, 
                 ROW_NUMBER() over (order by MatchId) as RN
FROM Matches 
WHERE LeagueStatueId = 280 AND Week <> 1 
ORDER BY Week, MatchDate, MatchTime
)
UPDATE cte SET MatchNumberCounter=RN+10008