我收到此错误 "The multi-part identifier "DCOT.Loyalty“无法绑定。”

I am getting this error "The multi-part identifier "DCOT.Loyalty" could not be bound."

我正在编写此代码以将忠诚度和获取列设置为 0 或 1。我收到一个错误:

The multi-part identifier "DCOT.Loyalty" could not be bound

我在这里做错了什么?在我看来,连接存在一些问题。有人可以帮我纠正这个问题吗?

DECLARE @CustomerSKID int
SET @CustomerSKID = (SELECT COUNT(CustomerSKID) 
                    FROM Dim.DimCustomerOwnedTractor)

WHILE (@CustomerSKID) > 1
BEGIN
    IF OBJECT_ID('tempdb..#Mintable1') IS NOT NULL
    BEGIN
        DROP TABLE #Mintable1
    END

    IF OBJECT_ID('tempdb..#Mintable2') IS NOT NULL
    BEGIN
        DROP TABLE #Mintable2
    END

    SELECT 
        CustomerSKID, MIN(PurchaseDate) AS MinPDate 
    INTO 
        #Mintable1 
    FROM 
        Dim.DimCustomerOwnedTractor 
    WHERE 
        ManufacturerName = 'Mahindra' 
    GROUP BY
        CustomerSKID
                        
    SELECT CustomerSKID 
    INTO #Mintable2 
    FROM #Mintable1 
    WHERE MinPDate IS NOT NULL

    UPDATE Dim.DimCustomerOwnedTractor
    SET DCOT.Loyalty = 1
    FROM Dim.DimCustomerOwnedTractor DCOT
    LEFT OUTER JOIN #Mintable2 AS M2 ON DCOT.CustomerSKID = M2.CustomerSKID
                                     AND DCOT.ManufacturerName = 'Mahindra'

    IF OBJECT_ID('tempdb..#Mintable3') IS NOT NULL
    BEGIN
        DROP TABLE #Mintable3
    END

    IF OBJECT_ID('tempdb..#Mintable4') IS NOT NULL
    BEGIN
        DROP TABLE #Mintable4
    END

    SELECT CustomerSKID, MAX(PurchaseDate) AS MaxPDate 
    INTO #Mintable3 
    FROM Dim.DimCustomerOwnedTractor 
    WHERE ManufacturerName = 'Mahindra' 
    GROUP BY CustomerSKID
                        
    SELECT CustomerSKID 
    INTO #Mintable4 
    FROM #Mintable3 
    WHERE MaxPDate IS NOT NULL

    UPDATE Dim.DimCustomerOwnedTractor
    SET DCOT.Acquisition = 1
    FROM Dim.DimCustomerOwnedTractor DCOT
    LEFT OUTER JOIN #Mintable2 AS M2 ON DCOT.CustomerSKID = M2.CustomerSKID
    LEFT OUTER JOIN #Mintable4 AS M4 ON DCOT.CustomerSKID = M4.CustomerSKID
    LEFT OUTER JOIN #Mintable3 AS M3 ON DCOT.CustomerSKID = M3.CustomerSKID
                                     AND DCOT.ManufacturerName = 'Mahindra'
                                     AND DCOT.PurchaseDate = M3.MaxPDate

    UPDATE Dim.DimCustomerOwnedTractor
    SET DCOT.Acquisition = 0
    FROM Dim.DimCustomerOwnedTractor DCOT
    LEFT OUTER JOIN #Mintable1 AS M1 ON DCOT.CustomerSKID = M1.CustomerSKID
    LEFT OUTER JOIN #Mintable4 AS M4 ON DCOT.CustomerSKID = M4.CustomerSKID 
                                     AND DCOT.ManufacturerName != 'Mahindra'
                                     AND DCOT.PurchaseDate = M1.MinPDate
END

在 SQL 服务器中正确使用 table 别名是

UPDATE DCOT
SET DCOT.Loyalty = 1
FROM Dim.DimCustomerOwnedTractor DCOT
..

在 FROM 子句中分配给 Dim.DimCustomerOwnedTractor 的 table 别名被指定为 UPDATE 子句中的目标对象。

您也可以在 SET 子句中省略 table 别名

UPDATE DCOT
SET Loyalty = 1
FROM Dim.DimCustomerOwnedTractor DCOT
..

您的 UPDATE 应该引用您的别名,例如 UPDATE DCOT,或者您可以从集合中删除别名,例如 SET Loyalty = 1

由于不匹配,您要求 SQL 更新名为 Dim.DimCustomerOwnedTractor 的 table 并告诉 SQL 使用 DCOT 而不是那个名字.