[英]How to specify ORDER BY when using DELETE FROM in combination with OUTPUT?
I use the query below to get the next item of a specific type from my table. 我使用下面的查询从我的表中获取特定类型的下一项。 I chose this query format to prevent racing conditions if several servers try to get the next item.
如果多个服务器试图获取下一个项目,我选择此查询格式以防止竞争条件。 Question: how can I get the item that has the lowest dtmLastRunDate?
问题:如何获得具有最低dtmLastRunDate的项目? I tried to add an "ORDER BY dtmLastRunDate" but it gives me "Incorrect Syntax near the keyword 'ORDER'".
我试图添加一个“ORDER BY dtmLastRunDate”,但它给了我“关键字'ORDER'附近的语法不正确”。
DELETE TOP(1) FROM Schedule
WITH (READPAST)
OUTPUT DELETED.intUserID, DELETED.dtmLastRunDate
WHERE intScheduleType = @intScheduleType
Put it into a CTE as below. 将其放入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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.