带子查询的TSQL更新

时间:2013-06-12 17:51:18

标签: tsql sql-server-2008-r2 updating

我创建了以下更新表现不佳。我觉得可能有一种更简单或更有效的方式来进行查询,而不是我的暴力尝试。请帮忙。

Update LocalServers 
set MustIPL = 1 
where LocalServerID IN (SELECT distinct olu.LocationID 
                        from OfferLocUpdate olu with (NoLock) 
                        Join LocalServers ls WITH (NOLOCK) on olu.LocationID = ls.LocationID 
                        Join CPE_IncentiveDLBuffer DLB with (NoLock) on olu.OfferID = dlb.OfferID 
                        where 
                             olu.LastSent > IncentiveLastHeard 
                             and CreationDate < dateadd(d, -@Days, getdate()) 
                             and DLB.FileName LIKE '%IDL-A')

1 个答案:

答案 0 :(得分:1)

试试这个:

    DECLARE @DateNow DATETIME
    SET @DateNow = GETDATE()

    ;WITH cte_DLB (OfferID, [FileName]) AS
    (
        SELECT OfferID, [FileName]
        FROM CPE_IncentiveDLBuffer (NOLOCK)
        WHERE  DLB.[FileName] LIKE '%IDL-A'
    )
    UPDATE 
        LS
    SET 
        LS.MustIPL = 1
    FROM 
        LocalServers LS 
        JOIN OfferLocUpdate OLU (NOLOCK) ON LS.LocalServerID = OLU.LocationID
        JOIN LocalServers ls (NOLOCK) ON OLU.LocationID = LS.LocationID 
        JOIN cte_DLB ON OLU.OfferID = cte_DLB.OfferID
    WHERE 
        OLU.LastSent > IncentiveLastHeard 
        AND CreationDate < dateadd(d, -@Days, @DateNow)

如果您澄清哪个表属于“IncentiveLastHeard”列,那将非常有用。 'CreationDate'