Jay*_*Jay 8 sql sql-server-2008
我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)
运行此语句将返回错误'视图或函数'cte'不可更新,因为修改会影响多个基表.
我理解为什么会抛出错误,我无法弄清楚如何解决它.任何建议将不胜感激!
Mik*_*son 20
您可以在CTE中使用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;
Run Code Online (Sandbox Code Playgroud)
像这样的东西?
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 queue
Where ID in (Select Id from cte)
Run Code Online (Sandbox Code Playgroud)