6

我想做一个 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)
4

3 回答 3

8

我认为将数据放在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
于 2013-01-15T12:57:07.583 回答
3

您不能UPDATE使用SELECT相同的 SQL 语句。但是,您可以像这样选择 ID 而不是UPDATEJOIN

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子句。

于 2013-01-15T12:53:46.867 回答
1

像这样的东西?

更新 WebHookLog
设置处理 = 1
WHERE WebHookLogID IN ( SELECT WebHookLogIDs = FROM WebHookLog
                WHERE 处理=0
                    AND 已处理=0
                        和暂停=0)

从 WebHookLog 中选择 *
WHERE WebHookLogID IN(WebHookLogIDs)

于 2013-01-15T12:52:56.727 回答