我有条件需要使用 MERGE 。 如果我们有匹配的记录,那么我们需要先删除该东西,然后插入它。你能指导我怎么做吗?这是我的查询,但我收到错误
> An action of type 'INSERT' is not allowed in the 'WHEN MATCHED' clause of a MERGE statement.
我同意是正确的。请帮我解决一下。
这是我的代码:
MERGE EntryInvoiceReference targetTable
USING(
Select * from @InvoiceReference inr join @HeaderInvoice_Temp hit
on inr.[EntryInvoice_Id]=hit.InvoiceId
) sourceTable
on targetTable.[EntryInvoice_Id]=sourceTable.Invoice_Id_Created
WHEN MATCHED
THEN DELETE
WHEN MATCHED
THEN INSERT (
[EntryInvoice_Id]
,[CIReferenceType]
,[CIReferenceValue]
)
VALUES(
sourceTable.Invoice_Id_Created,
sourceTable.[CIReferenceType],
sourceTable.[CIReferenceValue]
)
WHEN NOT MATCHED
THEN INSERT (
[EntryInvoice_Id]
,[CIReferenceType]
,[CIReferenceValue]
)
VALUES(
sourceTable.Invoice_Id_Created,
sourceTable.[CIReferenceType],
sourceTable.[CIReferenceValue]
);