title: Syntax error in SQL job in Azure SQL database
body: <p>I am inserting a job step in Azure Elastic job agent. This is my code:</p>
  <pre><code>EXEC jobs.sp_add_jobstep @job_name='Sample T-SQL',
  @command=N' declare @columns varchar(max)
  select @columns = stuff(( select '],[' + [$Name]
                      from dbo.groups
                      order by '],[' + convert(varchar(max), [$_ClosingBalance]) desc
                      for xml path('')), 1, 2, '' + ']'
  INSERT INTO dbo.Trial1  
  (   [Bank Accounts],[Bank OD A/c],[Branch / Divisions],[Capital Account],[Cash-in-Hand],[Current Assets],[Current Liabilities],[Deposits (Asset)]
        ,[Direct Expenses],[Direct Incomes],[Duties &amp; Taxes],[Fixed Assets],[Duties &amp; Taxes1],[Duties &amp; Taxes2],[Indirect Expenses],[Indirect Incomes],[Investments]
        ,[Loans &amp; Advances (Asset)],[Loans (Liability)],[Misc# Expenses (ASSET)],[Provisions],[Purchase Accounts],[Reserves &amp; Surplus],[Sales Accounts],[Secured Loans]
        ,[Stock-in-Hand],[Stock Transfer Outward],[Sundry Creditors],[Sundry Debtors],[Suspense A/c],[Duties &amp; Taxes3]
        ,[Unsecured Loans]
  )  
  Select * from (
      Select [$_ClosingBalance], RowN = Row_Number() over (order by @columns) from dbo.Groups
      ) a
      pivot (max([$_ClosingBalance]) for RowN in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31],[32])) p
  
   
  
  INSERT INTO dbo.Date ([Date]) VALUES (GETDATE())
  INSERT INTO dbo.Final
  SELECT * 
  FROM  dbo.Trial1
  INNER JOIN dbo.Date
  ON dbo.Trial1.IdCol = dbo.Date.IDCol1'',
  @credential_name='JobRun',
  @target_group_name='DatabaseGroup5'
  </code></pre>
  <p>I am receiving the following error :</p>
  <pre><code>Msg 102, Level 15, State 1, Line 3
  Incorrect syntax near ']'.
  Msg 137, Level 15, State 2, Line 15
  Must declare the scalar variable &quot;@columns&quot;.
  Msg 102, Level 15, State 1, Line 24
  Incorrect syntax near ''
  </code></pre>
  <p>My code is ending where it should not. Please help.</p>