title: Bulk upsert (update or insert) of a stored procedure
body: <p>I am trying to do a bulk upsert (update or insert) in a stored procedure into a SQL Server database and I am using sequelizer to connecting the SQL Server database using Node js.</p>
  <p>This is dataset is anywhere between 1-50k rows. Any help would be greatly appreciated!</p>
  <p>I am trying this in nodejs using sequelizer</p>
  <pre><code>const upsertedData = [
    {
      id: &quot;123&quot;,
      value: &quot;Value 2&quot;
    },
    {
      id: &quot;124&quot;,
      value: &quot;Value 1&quot;
    }
  ]
  
  const upsert = async (data) =&gt; {
    await sequelize.query(
      `BEGIN
        DECLARE @json NVARCHAR(MAX);
        SET @json = :dataToBeUpserted
        MERGE INTO dbo.tableToBeUpserted AS Target
        USING (SELECT * from OpenJson(@json) WITH (
            id nvarchar(32),
            value nvarchar(32),
        )) AS Source
        ON (Target.id = Source.id)
        WHEN MATCHED THEN
        UPDATE SET 
            Target.value = Source.value
        WHEN NOT MATCHED THEN           
            INSERT (id, value)
            VALUES (Source.id, Source.value);
      END`,
      {
        replacements: {
          dataToBeUpserted: JSON.stringify(data)
        },
      }
    )
  }
  </code></pre>
  <p>upsert(upsertedData)</p>
  <p>However, I get this error:</p>
  <blockquote>
  <p>Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.</p>
  </blockquote>
  <p>and I even tried in SQL Server Management Studio:</p>
  <pre><code>DECLARE @json NVARCHAR(MAX);
  SET @json = N'[
    {&quot;id&quot;: 2, &quot;info&quot;: {&quot;name&quot;: &quot;John&quot;, &quot;surname&quot;: &quot;Smith&quot;}, &quot;age&quot;: 25},
    {&quot;id&quot;: 5, &quot;info&quot;: {&quot;name&quot;: &quot;Jane&quot;, &quot;surname&quot;: &quot;Smith&quot;}, &quot;dob&quot;: &quot;2005-11-04T12:00:00&quot;}
  ]';
  
  SELECT *
  FROM OPENJSON(@json)
    WITH (
      id INT 'strict $.id',
      firstName NVARCHAR(50) '$.info.name',
      lastName NVARCHAR(50) '$.info.surname',
      age INT,
      dateOfBirth DATETIME2 '$.dob'
    );
    
  </code></pre>
  <p>Getting this same error:</p>
  <blockquote>
  <p>Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.</p>
  </blockquote>
  <p>Please help me resolve this issue.</p>