我使用下面的查询从表中获取特定类型的下一项。
我选择了这种查询格式,以防止在多个服务器尝试获取下一个项目时出现竞争情况。
问题:如何获得dtmLastRunDate最低的项目?我试图添加“ ORDER BY dtmLastRunDate”,但它给我“关键字“ ORDER”附近的语法不正确”。
DELETE TOP(1) FROM Schedule
WITH (READPAST)
OUTPUT DELETED.intUserID, DELETED.dtmLastRunDate
WHERE intScheduleType = @intScheduleType
最佳答案
如下所示将其放入CTE。
;WITH T
AS (SELECT TOP(1) *
FROM Schedule WITH (ROWLOCK, READPAST)
WHERE intScheduleType = @intScheduleType
ORDER BY dtmLastRunDate)
DELETE FROM T
OUTPUT DELETED.intUserID,
DELETED.dtmLastRunDate