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? I tried to add an “ORDER BY dtmLastRunDate” but it gives me “Incorrect Syntax near the keyword ‘ORDER'”.
DELETE TOP(1) FROM Schedule
WITH (READPAST)
OUTPUT DELETED.intUserID, DELETED.dtmLastRunDate
WHERE intScheduleType = @intScheduleType
Put it into a CTE as below.