注意:我从sql表导入数据。在此之后我想在表中插入数据,但是当它找到相同的ContractNo(合同号)时,它将更新数据,当它找到不同的ContractNo(合同号)时,它将作为新记录插入
但我的合并查询无法使用where语句。
string mergeSql = "merge into " + tableName + " as Target " +
"using Ro_Consumers_Temp as Source " +
"on " +
"Target.ContractNo=Source.ContractNo " +
"when not matched then " +
"insert values (Source.Ronumber,Source.ContractNo,Source.BusinessPartner,Source.ContractAccount,Source.IBC,Source.Portion,Source.MRU,Source.Installation,Source.MeterNo,Source.LegacyNumber,Source.ConsumerNo,Source.ConsumerName,Source.Address,Source.Tariff,Source.ROAgent,Source.IBCName,Source.CD,Source.Batch,Source.JasbNumber,Source.SheetNo, Source.ContactName,Source.ContactNumber,Source.FOName,Source.[Address&LandMark],Source.NatureOfBusiness)" +
"when matched then update set Batch = Source.Batch, JasbNumber = Source.JasbNumber Where Target.Batch=Source.Batch;";
答案 0 :(得分:0)
将WHERE
子句替换为其他WHEN MATCHED
子句条件:
string mergeSql = "merge into " + tableName + " as Target " +
"using Ro_Consumers_Temp as Source " +
"on " +
"Target.ContractNo=Source.ContractNo " +
"when not matched then " +
"insert values (Source.Ronumber,Source.ContractNo,Source.BusinessPartner,Source.ContractAccount,Source.IBC,Source.Portion,Source.MRU,Source.Installation,Source.MeterNo,Source.LegacyNumber,Source.ConsumerNo,Source.ConsumerName,Source.Address,Source.Tariff,Source.ROAgent,Source.IBCName,Source.CD,Source.Batch,Source.JasbNumber,Source.SheetNo, Source.ContactName,Source.ContactNumber,Source.FOName,Source.[Address&LandMark],Source.NatureOfBusiness)" +
"when matched " +
"and Target.Batch=Source.Batch " + // <<< Move the clause here
"then update set Batch = Source.Batch, JasbNumber = Source.JasbNumber";
有关详细信息,请参阅MERGE
声明文档。