10

我们有许多存储过程,BizTalk 使用这些存储过程从队列(和连接表)中获取n行并同时更新这些项目的状态。

我正在尝试修改这些查询以删除表变量的使用,而是在单个语句中完成工作。我已经成功了一些,但是这个特定的例子很棘手,因为joinCTE 中有一个,我想返回一些连接的列,即使它们不是update.

这就是我想出的:

;with q as
(
    select top (@FetchCount) 
        iq.InQueueId,
        itk.[Message],
        iq.PatNo,
        iq.DistrictNo,
        itk.Interaction,
        iq.[Status]
    from
        InQueue iq
        join Itk on iq.InQueueId = itk.InQueueId
        join [Endpoint] e on iq.[Endpoint] = e.EndpointId
        join EndpointName en on en.EndpointNameId = e.Name
    where
        en.Name = 'XYZ'
        and iq.[Status] = @StatusNew
    order by
        iq.InQueueId
)
update
    q
set
    [Status] = @StatusSelected
output
    inserted.InQueueId as [Id],
    inserted.[Message] as [Msg],
    inserted.DistrictNo,
    inserted.Interaction

这立即失败并出现以下错误:

不允许列引用“inserted.Message”,因为它引用了未在此语句中修改的基表。

显然,这是因为MessageInteraction列不能作为inserted集合的一部分返回,因为它们位于不同的表中,因此没有更新。

所以我试图将output条款更改为:

output
    inserted.InQueueId as [Id],
    q.[Message] as [Msg],
    inserted.DistrictNo,
    q.Interaction

这失败并出现错误:

无法绑定多部分标识符“q.Message”。

是否可以在不重写查询以使用临时表或表变量的情况下实现这一点?

4

1 回答 1

14

您可以使用deleted输出子句中的集合来引用未更新表中 CTE 中的列

例如

output
    inserted.InQueueId as [Id],
    deleted.[Message] as [Msg],
    inserted.DistrictNo,
    deleted.Interaction

an 中的deletedand也可以被认为是 before 和 after,尽管这里的术语匹配andinsertedupdatedelete...outputinsert...output

于 2013-07-23T13:42:17.010 回答