简体   繁体   English

如何在将DELETE FROM与OUTPUT结合使用时指定ORDER BY?

[英]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.

相关问题 在插入语句中使用OUTPUT时,如果您在select中指定顺序,是否遵循select中指定的顺序? - When using OUTPUT in an insert statement, if you specify the order by in the select does it honor the order specified in the select? 使用表值构造函数时如何指定返回查询的顺序 - How to specify order of returned query when using Table Value Constructor 使用** DELETE FROM…OUTPUT…INTO **时,获取目标表的标识 - Get Identity of destination table when using **DELETE FROM … OUTPUT … INTO** 使用按顺序和限制从多个表中删除 - Delete from multiple tables using order by and limit (1093, u“您不能在 FROM 子句中指定目标表 'wallet_consume_record' 进行更新”) 在 mysql 中使用删除时 - (1093, u“You can't specify target table 'wallet_consume_record' for update in FROM clause”) when using delete in mysql 如何使用 Union All function 指定 BigQuery 中显示的数据顺序 - How to specify the order of data displayed in BigQuery using the Union All function 如何在 MySQL 中从比较和 null 检查中指定 ORDER BY? - How to specify an ORDER BY from comparisons and null checks in MySQL? 使用按钮删除由两个表组合而成的行 - Delete row that is combination of two tables using button 将 FETCH NEXT X ROWS ONLY 与 ORDER BY 结合使用时,Oracle 未返回预期结果 - Oracle not returning expected results when using FETCH NEXT X ROWS ONLY in combination with ORDER BY 如何使用order by获取需求输出? - How to get require output using order by?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM