我的合并语句失败
My Merge statement is failing
我有一个合并语句,用于将值插入数据库。我遇到异常错误的异常点
A MERGE statement must be terminated by a semi-colon (;).
这是我的声明。谁能指出我遗漏了什么?
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();
}
如错误所述,在语句末尾添加一个分号。所以你的执行应该如下所示。
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
});
我有一个合并语句,用于将值插入数据库。我遇到异常错误的异常点
A MERGE statement must be terminated by a semi-colon (;).
这是我的声明。谁能指出我遗漏了什么?
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();
}
如错误所述,在语句末尾添加一个分号。所以你的执行应该如下所示。
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
});