更新 Table 不存在的变量

Update a Table Variable Where Not Exists

UPDATE 
    @Customer
SET 
    ValidaitonAction = 1
WHERE NOT EXISTS
    (SELECT 1 FROM DMScustomerupload WHERE AccountNumber = @Customer.AccountNumber)

其中 @Customer 是一个 TABLE 变量:

DECLARE @Customer TABLE ( ID int,
                        ValidaitonAction int,
                        ... other columns))

在最后一行,我得到

Must declare scalar variable @Customer

您需要一个 table 别名:

UPDATE c
    SET ValidationAction = 1
    FROM @Customer c
    WHERE NOT EXISTS (SELECT 1 FROM DMScustomerupload cu WHERE cu.AccountNumber = c.AccountNumber);

SQL 服务器自动将任何以 @ 开头的内容解释为变量(某种)。 Table 别名是另一回事,因此找不到匹配项。