如何在SQL Server

时间:2015-09-02 14:34:18

标签: sql-server sql-update sql-server-2014 table-variable

我在存储过程中有一个UPDATE查询,我需要更新已经由某些数据填充的表变量。如下面的代码中那样,该查询由于在子查询中引用表变量而引起错误,即在子查询中引用@daysPerEmail.ProductId。我尝试在UPDATE @daysPerEmail d SET LastEmailDate...中使用表变量的别名,但它没有帮助。

问题:如何重新编写查询,以便在子查询中正确引用ProductId的表变量列?

UPDATE @daysPerEmail SET LastEmailDate = (SELECT max(ActionDate) FROM 
        useractionsreport  WHERE ProductId = @daysPerEmail.ProductId 
       and ActionTypeId = @userActionTypeId );

更新1

要在SSMS中重现这一点,我使用以下查询。 SSMS提供错误Must declare the scalar variable "@daysPerEmail".

DECLARE @daysPerEmail TABLE (
    VendorId INT,
    DaysPerEmail SMALLINT,
    LastEmailDate DATE,
    ProductId INT 
)

INSERT INTO @daysPerEmail (VendorId, DaysPerEmail, LastEmailDate, ProductId)
    VALUES (10, 5, NULL, 11), (12, 3, NULL, 15), (14, 1, NULL, 22);

UPDATE @daysPerEmail
SET LastEmailDate = (SELECT
    MAX(ActionDate)
FROM UserActionsReport   
WHERE ProductId = @daysPerEmail.ProductId
AND ActionTypeId = 2);

更新2

Paolo的答案按预期工作,但我也找到了另一个使用查询的简单解决方案,如下所示。我所要做的就是给@daysPerEmail表变量中的ProductId列赋一个唯一的名称。我将名称更改为ProdId,这在UPDATE查询中涉及的任何表中都不存在。然后我没有必要在子查询中引用表变量,因为ProdId列名只能在UPDATE查询中使用的表中的@daysPerEmail表变量中找到。

 DECLARE @daysPerEmail TABLE (
    VendorId INT,
    DaysPerEmail SMALLINT,
    LastEmailDate DATE,
    ProdId INT 
)

INSERT INTO @daysPerEmail (VendorId, DaysPerEmail, LastEmailDate,ProdId)
    VALUES (78, 5, NULL, 2), (78, 3, NULL, 387), (78, 1, NULL, 295);

UPDATE @daysPerEmail
SET LastEmailDate = (SELECT
    MAX(ActionDate)
FROM UserActionsReport   
WHERE ProductId = ProdId
AND ActionTypeId = 2);

2 个答案:

答案 0 :(得分:2)

加入更新怎么样?
如果我没记错,它不符合标准,但SQL-Server允许:

UPDATE  X
SET     LastEmailDate = Z.MaxActionDate
FROM    @daysPerEmail AS X
        JOIN (
            SELECT  MAX(Y.ActionDate) AS MaxActionDate
                    , Y.ProductId
            FROM    UserActionsReport AS Y
            WHERE   ActionTypeId = 2
            GROUP BY Y.ProductId
        ) AS Z ON Z.ProductId = X.ProductId;

答案 1 :(得分:2)

使用方括号,例如 [@ daysPerEmail] ,而不是 @daysPerEmail

这样你可以使用表变量的别名。