这是在添加额外匹配参数AND PR.PaymentRecommendationId = InputJSON.PaymentRecommendationId)
后在更改操作时抛出异常的存储过程:
ALTER PROCEDURE usp_PaymentRecommendationsInsertUpdate
@PaymentRecommendationsJson NVARCHAR(MAX)
AS
BEGIN
MERGE INTO PaymentRecommendations AS PR
USING (
SELECT ClaimId,
CoverageTypeId,
Payee,
IndemnityPmtRequestAmnt,
ExpensePaymentRequestAmnt,
RequestDate,
Memo,
CheckNumber,
IssueDate,
CreatedOn,
CreatedBy,
ModifiedOn,
ModifiedBy
FROM OPENJSON(@PaymentRecommendationsJson)
WITH (
PaymentRecommendationId BIGINT,
ClaimId BIGINT,
CoverageTypeId SMALLINT,
Payee VARCHAR(256),
IndemnityPmtRequestAmnt MONEY,
ExpensePaymentRequestAmnt MONEY,
RequestDate DateTime,
Memo VARCHAR(512),
CheckNumber VARCHAR(16),
IssueDate DATETIME,
CreatedOn DATETIME,
CreatedBy BIGINT,
ModifiedOn DATETIME,
ModifiedBy BIGINT
)) AS InputJSON
ON (PR.ClaimId = InputJSON.ClaimId AND PR.PaymentRecommendationId = InputJSON.PaymentRecommendationId)
WHEN MATCHED THEN
UPDATE SET ClaimId = InputJSON.ClaimId,
CoverageTypeId = InputJSON.CoverageTypeId,
Payee = InputJSON.Payee,
IndemnityPmtRequestAmnt = InputJSON.IndemnityPmtRequestAmnt,
ExpensePaymentRequestAmnt = InputJSON.ExpensePaymentRequestAmnt,
RequestDate = InputJSON.RequestDate,
Memo = InputJSON.Memo,
CheckNumber = InputJSON.CheckNumber,
IssueDate = InputJSON.IssueDate,
ModifiedOn = GETUTCDATE(),
ModifiedBy = InputJSON.ModifiedBy
WHEN NOT MATCHED THEN
INSERT ( ClaimId,
CoverageTypeId,
Payee,
IndemnityPmtRequestAmnt,
ExpensePaymentRequestAmnt,
RequestDate,
Memo,
CheckNumber,
IssueDate,
CreatedOn,
CreatedBy)
VALUES ( InputJSON.ClaimId,
InputJSON.CoverageTypeId,
InputJSON.Payee,
InputJSON.IndemnityPmtRequestAmnt,
InputJSON.ExpensePaymentRequestAmnt,
InputJSON.RequestDate,
InputJSON.Memo,
InputJSON.CheckNumber,
InputJSON.IssueDate,
GETUTCDATE(),
InputJSON.CreatedBy)
WHEN NOT MATCHED BY SOURCE THEN
DELETE;
END
以下行导致上述存储过程出错:
ON (PR.ClaimId = InputJSON.ClaimId AND PR.PaymentRecommendationId = InputJSON.PaymentRecommendationId)
错误:Invalid column name 'PaymentRecommendationId'.
但是PaymentRecommendationId
列在json字符串中传递以及它存在于表