I have a following query:
UPDATE TOP (@MaxRecords) Messages
SET status = \'P\'
OUTPUT inserted.*
FROM Messages
where Status = \'N\'
and InsertDate &
the correct syntax of update is
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
You can try sub query like
UPDATE Messages
SET status = 'P'
WHERE MessageId IN (SELECT TOP (@MaxRecords) MessageId FROM Messages where Status = 'N' and InsertDate >= GETDATE() ORDER BY Priority)
output inserted.*
you can use common table expression for this:
;with cte as (
select top (@MaxRecords)
status
from Messages
where Status = 'N' and InsertDate >= getdate()
order by ...
)
update cte set
status = 'P'
output inserted.*
This one uses the fact that in SQL Server it's possible to update cte, like updatable view.