使用MERGE时出现SQL71501错误

时间:2018-02-28 15:37:25

标签: sql-server visual-studio database-project

我将现有数据库导入Visual Studio数据库项目(VS2017)。其中一个存储过程使用具有MERGE子句的WHEN NOT MATCHED THEN INSERT语句。

不幸的是,这会导致Visual Studio出现问题,而这似乎无法解决语句VALUES部分中的某些列。这是确切的错误:

  

错误SQL71501:过程:[dbo]。[spOvernightImportRating]包含对象的未解析引用。对象不存在或引用不明确,因为它可以引用以下任何对象:[dbo]。[tblAmountType]。[AmountTypeID],[dbo]。[tblAmountType]。[AmtType] :: [AmountTypeID] ,[dbo]。[tblImportPolicy]。[AmtType] :: [AmountTypeID],[dbo]。[tblImportRating]。[AmtType] :: [AmountTypeID],[dbo]。[tblImportSettings]。[AmtType] :: [AmountTypeID ],[dbo]。[tblPolicy]。[AmtType] :: [AmountTypeID],[dbo]。[tblPolicy_EffectiveDate]。[AmtType] :: [AmountTypeID]或[dbo]。[tblPolicyGenerationDetail]。[AmountTypeID]。 MGA_BMS.DB
  C:\ Users \ tomh \ Documents \ Visual Studio 2017 \ Projects \ MGA_BMS.DB \ MGA_BMS.DB \ Stored Procedures \ spOvernightImportRating.sql 77

代码的相关部分:

MERGE dbo.tblPolicyGenerationDetail
        USING
        (
            SELECT 
                PGM.MaxPolicyGenerationID AS PolicyGenerationID,
                AmtType.AmountTypeID,
                R.ClassCode,
                R.[Description],
                CAST(COALESCE(R.Amount, '0.00') AS DECIMAL(14, 6)) AS Amount,
                CASE WHEN COALESCE(R.ApplyMode, '0') = '0' THEN 0 ELSE 1 END AS ApplyMode,
                CAST(COALESCE(R.ModifiedAmount, '0.00') AS DECIMAL(14, 6)) AS ModifiedAmount,
                CAST(COALESCE(R.CommissionMGA, '0.00') AS DECIMAL(14, 6)) AS CommissionMGA,
                CAST(COALESCE(R.CommissionBroker, '0.00') AS DECIMAL(14, 6)) AS CommissionBroker,
                CAST(COALESCE(R.NetPremium, '0.00') AS DECIMAL(14, 6)) AS NetPremium,
                P.PolicyID,
                PED.PolicyEffDateID
            FROM
                dbo.tblImportRating R
            INNER JOIN dbo.tblImportPolicy IP ON
                IP.IsProcessed = 0 AND
                IP.IsValid = 1 AND
                IP.TransactionID = R.TransactionID AND
                COALESCE(IP.RecordType, '') = 'P' AND
                IP.Transaction_Type IN ('New', 'Reinstate', 'Renew')
            INNER JOIN dbo.tblImportSettings S ON S.ProgramID = IP.PROGRAM_ID
            INNER JOIN dbo.tblPolicy P ON P.CarrierPolicyID = IP.CARRIER_POLICY_ID 
                AND P.ProgramID = CAST(IP.Program_ID AS INT)
                AND (R.MGA_POLICY_ID = P.MGAPolicyID or(S.RequireMgaPolicyID = 0 AND coalesce(R.MGA_POLICY_ID, '') = ''))
            INNER JOIN dbo.tblPolicy_EffectiveDate PED ON PED.PolicyID = P.PolicyID AND PED.TermNumber = CAST(IP.TermNumber AS INT)
            INNER JOIN (SELECT PG.PolicyEffDateID, MAX(PG.PolicyGenerationID) AS MaxPolicyGenerationID FROM dbo.tblPolicyGeneration PG GROUP BY PG.PolicyEffDateID) PGM ON PGM.PolicyEffDateId = PED.PolicyEffDateId
            INNER JOIN dbo.tblAmountType AmtType ON AmtType.CalculationType = R.AmountType
            WHERE
                R.IsProcessed = 0 AND
                R.IsValid = 1 AND
                CASE R.AmountType
                    WHEN 'Class' THEN S.AggregateClass
                    WHEN 'Premium' THEN S.AggregatePremium
                    WHEN 'Fee' THEN S.AggregateFee
                    WHEN 'Tax' THEN S.AggregateTax
                    WHEN 'Deposit' THEN S.AggregateDeposit
                END = 0 --Only Control records
        ) AS [source] ON 1 = 0
        WHEN NOT MATCHED THEN INSERT (PolicyGenerationID, AmountTypeID, ClassCode, [Description], Amount, ApplyMode, ModifiedAmount, CommissionMGA, CommissionBroker, NetPremium)
        VALUES (PolicyGenerationID, AmountTypeID, ClassCode, [Description], Amount, ApplyMode, ModifiedAmount, CommissionMGA, CommissionBroker, NetPremium)
        OUTPUT INSERTED.PolicyGenerationDetailID, INSERTED.AmountTypeID, [source].PolicyID, [source].PolicyEffDateID INTO @IDs;

我已经就这类错误阅读了其他几篇文章,但似乎没有一篇文章适用于这种特定情况。有什么建议?有没有人以前见过这个问题?

谢谢!

0 个答案:

没有答案