我正在尝试使用sql表实现FIFO队列。
我有以下SQL(为发布而修改),联接和参数的使用对于此过程的工作方式很重要。
With cte as ( select top(1) q.* from queue q with (readpast) inner join MyTable a on q.id = a.myTableID AND myTable.procID = @myParam order by q.Data asc ) delete from cte output deleted.ID, deleted.col1
运行此语句将返回错误“视图或函数’cte’不可更新,因为修改会影响多个基表”。
我了解为什么会引发错误,但我不知道该如何解决。任何建议将不胜感激!
您可以在CTE中使用exists()而不是内部MyTable联接。
exists()
MyTable
with cte as ( select top(1) q.id, q.col1 from queue q with (readpast) where exists( select * from MyTable a where q.id = a.myTableID AND a.procID = @myParam ) order by q.Data asc ) delete from cte output deleted.ID, deleted.col1;