SQL71501:过程包含对象的未解析引用

时间:2017-11-20 16:04:33

标签: sql sql-server visual-studio stored-procedures

我们有一个名为PrematureLoanSettlement的表:

CREATE TABLE [dim].[PrematureLoanSettlementRequest]
(
     [PrematureLoanSettlementRequestID]             INT             NOT NULL    IDENTITY(1,1) PRIMARY KEY
    ,[PrematureLoanSettlementRequestID_Primary]     BIGINT          NOT NULL
    ,[ContractID]                                   INT             NOT NULL    
    ,[RequestDateID]                                INT             NOT NULL

    ,[Created]                                      DATETIME        NOT NULL    DEFAULT GETDATE()
    ,[Modified]                                     DATETIME        NOT NULL    DEFAULT GETDATE()
)
GO

并且在ETL过程中我想更新一个ContractID:

--  Update ContractID
-------------------------------------------------------------------------

IF @DateID BETWEEN @MinDateID AND @MaxDateID
    BEGIN

        UPDATE stg.PrematureLoanSettlementRequest
        SET a.CountryId = b.ContractID
        SELECT ContractID, @SourceID, @CountryId

        FROM stg.PrematureLoanSettlementRequest a

                INNER JOIN dim.Contract b
                    ON a.ContractID = b.ContractID

        WHERE b.SourceID = @SourceID
            AND b.CountryId = @CountryId

        -- For existing
        UPDATE a
        SET a.ContractID = b.ContractID 
        FROM
            [stg].[PrematureLoanSettlementRequest] a

            JOIN [dim].[PrematureLoanSettlementRequest] b 
                ON a.[PrematureLoanSettlementRequestID_Primary] = b.[PrematureLoanSettlementRequestID_Primary]

        WHERE a.SourceID = @SourceID
            AND a.ContractID IS NULL        

        -- Others
        UPDATE [stg].[PrematureLoanSettlementRequest]
        SET ContractID = 19000101
        WHERE ContractID IS NULL

    END

返回错误:

JOIN [dim].[PrematureLoanSettlementRequest] b 
                    ON a.[PrematureLoanSettlementRequestID_Primary] = **b.[PrematureLoanSettlementRequestID_Primary]**
  

SQL71501:过程:[s01]。[stgPrematureLoanSettlementRequest]   包含对象的未解析引用。对象都可以   不存在或引用含糊不清,因为它可以引用任何引用   以下对象:   [DIM]。[PrematureLoanSettlementRequest]。[B] :: [PrematureLoanSettlementRequestID_Primary]   [DIM]。[PrematureLoanSettlementRequest]。[PrematureLoanSettlementRequestID_Primary]   要么   [STG] [PrematureLoanSettlementRequest]。[B] :: [PrematureLoanSettlementRequestID_Primary]。 Staging.Database C:\ Workspace \ Projects \ Staging.Database \ SchemaObjects \ Procedures \ s01 \ stgPrematureLoanSettlementRequest.sql

任何帮助都不胜感激。

1 个答案:

答案 0 :(得分:0)

您应该询问另一个问题,提供样本数据以及您想要做什么。我怀疑代码可以大大简化。

您当前代码中至少有一个问题是:

    UPDATE stg.PrematureLoanSettlementRequest
        SET a.CountryId = b.ContractID

ab都未定义。