我想做一个SELECT来获取ID列表,根据这些ID进行更新,然后返回这些记录。
我猜这样的事情,我只是不知道语法:
SELECT WebHookLogIDs = FROM WebHookLog
WHERE Processing=0
AND Processed=0
AND Paused=0
ORDER BY FailCount ASC, WebHookLogID DESC
UPDATE WebHookLog
SET Processing = 1
WHERE WebHookLogID IN(WebHookLogIDs)
SELECT * FROM WebHookLog
WHERE WebHookLogID IN(WebHookLogIDs)
答案 0 :(得分:5)
我认为将数据放在 Temp表中比在其中插入数据更好
因为最后你想要返回那些记录,所以你需要使用临时表
DECLARE @Table1 TABLE (WebHookLogIDs INT)
Insert into @Table1(WebHookLogIDs )
SELECT WebHookLogIDs FROM WebHookLog
WHERE Processing=0
AND Processed=0
AND Paused=0
ORDER BY FailCount ASC, WebHookLogID DESC
UPDATE WebHookLog
SET Processing = 1
WHERE WebHookLogID IN( select WebHookLogIDs from @Table1)
SELECT * FROM WebHookLog
WHERE WebHookLogID IN(select WebHookLogIDs from @Table1)
DROP TABLE @Table1
答案 1 :(得分:3)
您不能UPDATE
SELECT
使用相同的SQL语句。但是,UPDATE
可以使用JOIN
而不是像这样选择ID:
UPDATE w1
SET w1.Processing = 1
FROM WebHookLog w1
INNER JOIN WebHookLog w2 ON w1.WebHookLogID = w2.WebHookLogID
AND w2.Processing = 0
AND w2.Processed = 0
AND w2.Paused = 0;
稍后,您可以执行另一个SELECT
条款。
答案 2 :(得分:1)
这样的东西?
UPDATE WebHookLog SET PROCESSING = 1 WHERE WebHookLogID IN ( SELECT WebHookLogIDs = FROM WebHookLog WHERE Processing=0 AND Processed=0 AND Paused=0) SELECT * FROM WebHookLog WHERE WebHookLogID IN(WebHookLogIDs)