0

以下查询将为我提供运行此命令后立即执行更新所需的 ID 列表:

SELECT TOP 5 WorkflowEventProcessingID
FROM Master.WorkflowEventProcessing
WHERE ProcessingToken IS NULL
ORDER BY WorkflowEventProcessingId

在此处输入图像描述

有没有办法在一个选择语句中获取此范围的最小(55034)和最大(55038)ID,所以我可以这样做:

UPDATE WorkflowEventProcessing
SET ProcessingToken = <guid here>
WHERE WorkflowEventProcessingId >= @minId
  AND  WorkflowEventProcessingId <= @maxId

我尝试过这样的事情,但我已经走了:

DECLARE @minId INT
DECLARE @maxId INT

SELECT TOP 5 @minId = min(WorkflowEventProcessingID), @maxId = MAX(WorkflowEventProcessingID)
FROM Master.WorkflowEventProcessing
WHERE ProcessingToken IS NULL
ORDER BY WorkflowEventProcessingId

编辑

我不想使用子 SELECT 进行更新,因为我遇到了死锁。所以我被告知将 SELECT 和 UPDATE 分开,以便让单独的 DELETE 进程有机会在 UPDATE 和 SELECT 之间运行。

4

3 回答 3

1

考虑使用子查询而不是范围:

UPDATE  WorkflowEventProcessing
SET     ProcessingToken = <guid here>
WHERE   WorkflowEventProcessingId in
        (
        SELECT  TOP 5 WorkflowEventProcessingID
        FROM    Master.WorkflowEventProcessing with (updlock, holdlock)
        WHERE   ProcessingToken IS NULL
        ORDER BY 
                WorkflowEventProcessingId
        )
于 2012-08-17T15:04:01.743 回答
1

尝试

DECLARE @min INT
DECLARE @max INT

select @min = min(WorkflowEventProcessingID), 
       @max = max(WorkflowEventProcessingID)
from (
        SELECT TOP 5 WorkflowEventProcessingID
        FROM Master.WorkflowEventProcessing
        WHERE ProcessingToken IS NULL
        ORDER BY WorkflowEventProcessingId
     ) x

进而

UPDATE WorkflowEventProcessing
SET ProcessingToken = <guid here>
WHERE WorkflowEventProcessingId between @min and @max
于 2012-08-17T15:04:25.660 回答
0

这样做不是更容易吗?:

UPDATE WorkflowEventProcessing
SET ProcessingToken = <guid here>
WHERE WorkflowEventProcessingId IN (SELECT TOP 5 WorkflowEventProcessingID
FROM Master.WorkflowEventProcessing
WHERE ProcessingToken IS NULL
ORDER BY WorkflowEventProcessingId)

好的,看到您不能使用 a 的评论SELECTUPDATE尽管我真的不明白为什么不能使用),并假设 SQL Server 2005+,您可以尝试以下操作:

;WITH CTE AS
(
    SELECT TOP 5 WorkflowEventProcessingID
    FROM Master.WorkflowEventProcessing
    WHERE ProcessingToken IS NULL
    ORDER BY WorkflowEventProcessingId
)
UPDATE CTE 
SET ProcessingToken = <guid here>
于 2012-08-17T15:05:38.787 回答