1

I have a merge statement which is to insert values into a database. I hit an exception point with an exception error

A MERGE statement must be terminated by a semi-colon (;).

Here is my statement. Could anyone point out what am I missing please?

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();
}
James Z
  • 12,209
  • 10
  • 24
  • 44
Yuvi
  • 528
  • 8
  • 18
  • 2
    The message is pretty clear,add a semicolon at the end `..,SentEmail);",` – Mihai Jan 05 '16 at 13:36
  • @Mihai where are you asking me to add the ; as I already have one before the catch statement – Yuvi Jan 05 '16 at 13:38
  • Mihai is telling you to add the semicolon at the end of your String parameter in the conn.Execute() method; namely here: "VALUES(Id,CreatedAt,UpdatedAt,IsComplete,FormId,[Source],SentEmail)" + ";" – Bandreid Jan 05 '16 at 13:47
  • I am sorry that I did not catch it first time.@Mihai @bandreid – Yuvi Jan 05 '16 at 13:51

1 Answers1

2

As the error says add a semicolon to the end of your statement. So your Execute should look like the following.

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
    });
jradich1234
  • 1,410
  • 5
  • 24
  • 29