1

我有一个合并语句,它将值插入数据库。我遇到异常错误的异常点

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();
}
4

1 回答 1

2

正如错误所说,在语句的末尾添加一个分号。所以你的 Execute 应该如下所示。

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
    });
于 2016-01-05T13:40:19.353 回答