我有一个合并语句,用于将值插入数据库。我遇到异常错误
MERGE语句必须以分号(;)结束。
这是我的发言。有人能说出我想念的是什么吗?
using (var masterDb = new SqlConnection(ConfigurationManager.ConnectionStrings["MasterDB"].ConnectionString))
{
masterDb.Open();
if (data != null)
try
{
conn.Execute(
"MERGE INTO [dbo].[FormSubmissions] AS TARGET " +
"USING(VALUES(@Id,@CreatedAt,@UpdatedAt,@IsComplete,@FormId,[@Source],@SentEmail)) AS SOURCE (Id,CreatedAt,UpdatedAt,IsComplete,FormId,[Source],SentEmail) " +
"ON TARGET.Id = SOURCE.Id " +
"WHEN MATCHED THEN " +
"UPDATE SET CreatedAt = SOURCE.CreatedAt,UpdatedAt = SOURCE.UpdatedAt,IsComplete = SOURCE.IsComplete, FormId = SOURCE.FormId, [Source] = SOURCE.[Source], SentEmail = SOURCE.SentEmail " +
"WHEN NOT MATCHED BY TARGET THEN " +
"INSERT(Id,CreatedAt,UpdatedAt,IsComplete,FormId,[Source],SentEmail) " +
"VALUES(Id,CreatedAt,UpdatedAt,IsComplete,FormId,[Source],SentEmail)",
new
{
Id = formSubmissionsId,
CreatedAt = data.CreatedAt,
UpdatedAt = data.UpdatedAt,
IsComplete = data.IsComplete,
FormId = data.FormId,
Source = data.Source,
SentEmail = data.SentEmail
});
}
catch (Exception ex)
{
throw;
}
finally
{
masterDb.Close();
}
};
conn.Close();
}
答案 0 :(得分:2)
如错误所示,在语句末尾添加分号。因此,您的执行应如下所示。
conn.Execute(
"MERGE INTO [dbo].[FormSubmissions] AS TARGET " +
"USING(VALUES(@Id,@CreatedAt,@UpdatedAt,@IsComplete,@FormId,[@Source],@SentEmail)) AS SOURCE (Id,CreatedAt,UpdatedAt,IsComplete,FormId,[Source],SentEmail) " +
"ON TARGET.Id = SOURCE.Id " +
"WHEN MATCHED THEN " +
"UPDATE SET CreatedAt = SOURCE.CreatedAt,UpdatedAt = SOURCE.UpdatedAt,IsComplete = SOURCE.IsComplete, FormId = SOURCE.FormId, [Source] = SOURCE.[Source], SentEmail = SOURCE.SentEmail " +
"WHEN NOT MATCHED BY TARGET THEN " +
"INSERT(Id,CreatedAt,UpdatedAt,IsComplete,FormId,[Source],SentEmail) " +
"VALUES(Id,CreatedAt,UpdatedAt,IsComplete,FormId,[Source],SentEmail);",
new
{
Id = formSubmissionsId,
CreatedAt = data.CreatedAt,
UpdatedAt = data.UpdatedAt,
IsComplete = data.IsComplete,
FormId = data.FormId,
Source = data.Source,
SentEmail = data.SentEmail
});