SQL UPDATE使用不寻常的语法

时间:2018-06-04 05:00:50

标签: sql-server sql-server-2008 tsql sql-update

我今天遇到了这个存储过程,我对它的语法感到困惑。它正在进行更新,查询如下所示:

DECLARE @TransactionStatus tinyint = 1, 
        @RedeemedAmount decimal(18,2) = 0, 
        @PromotionCodeAmount decimal(18,2) = 0,
        @EventDetailId int = 0

UPDATE PED
    SET @RedeemedAmount = PED.RedeemedAmount = PED.RedeemedAmount + PE.PromoCodeAmount, 
        @PromotionCodeAmount = PE.PromoCodeAmount,   
        @EventDetailId = PED.Id
FROM dbo.PromotionEvents AS PE 
JOIN dbo.PromotionEventDetails AS PED ON PED.EventId = PE.Id
    WHERE PED.Id IN (
        SELECT TOP 1 PED.ID
        FROM dbo.PromotionEvents AS PE 
        JOIN dbo.PromotionEventDetails AS PED ON PED.EventId = PE.Id
        WHERE PE.Id = @EventId
    AND PED.Amount >= PED.RedeemedAmount + PE.PromoCodeAmount 
        AND PE.StartDate <= GETDATE() AND PE.EndDate > GETDATE()
        AND PE.MStatusId = 1
        AND PED.MStatusId = 1
        ORDER BY PED.CreatedDateTime)

这只是整个存储过程的一部分。我只需要了解这部分的作用吗?

SET @RedeemedAmount = PED.RedeemedAmount = PED.RedeemedAmount + PE.PromoCodeAmount, 

请注意,该行上有两个等号。

1 个答案:

答案 0 :(得分:2)

该行同时做两件事:

  • 更新PromotionEventDetails.RedeemedAmount
  • @RedeemedAmount变量
  • 中存储相同的值

这似乎是通常的UPDATE ... SET column = expression与SQL Server的{set 1}}的“set variable”语法的混合。测试显示无法执行SET @variable = expression@a = @b = col1,它仅适用于一个变量和一个目标列。