这个目前正在工作,但是我想更改update语句以包含它下面的插入操作,是不可能的?
UPDATE cas
SET [Locked] = CASE WHEN cas.Locked <> @TargetState AND cas.LastChanged = filter.SourceDateTime THEN @TargetState ELSE cas.[Locked] end,
OUTPUT inserted.Id, inserted.Locked, CASE WHEN inserted.Locked = @TargetState AND
inserted.LastChanged = filter.SourceDateTime THEN 1
WHEN inserted.LastChanged <> filter.SourceDateTime THEN -1 -- out of sync
WHEN deleted.Locked = @TargetState THEN -2 -- was not in a good state
ELSE 0 END --generic failure
INTO @OUTPUT
FROM dbo.Target cas WITH(READPAST, UPDLOCK, ROWLOCK) INNER JOIN @table filter ON cas.Id = filter.Id
INSERT INTO @OUTPUT
SELECT filter.id, NULL, when cas.id is not null -3 -- row was/is locked
else -4 end --not found
FROM @table filter left join dbo.target cas with(nolock) on filter.id = cas.id
WHERE NOT EXISTS (SELECT 1 FROM @OUTPUT result WHERE filter.id = result.UpdatedId)
答案 0 :(得分:0)
我不认为你想要的是什么。
所以,不,我认为无法在单个SQL语句中实现此目标。