title: Fix: Msg 102, Level 15, State 1, Line 37 Incorrect syntax near ')'
body: <p>I'm using MS SQL server.</p>
  
  <p>I have used <strong>pivot</strong> in my query. What I've done yet is given below:</p>
  
  <h2>SQL Procedure</h2>
  
  <pre><code>--/*==========================================================
  --
  --
  --
  --===========================================================*/
  
  --CREATE VIEW View_BASequenceExportDtl
  --AS
  
  --ALTER PROCEDURE Proc_BASequenceExportDtl 
  
  --AS
  
  DECLARE @cols NVARCHAR(MAX)
  DECLARE @stmt NVARCHAR(MAX)
  
  
  SET @cols = ISNULL(@cols + ', ', '') (
                                              SELECT Table3.vAnalyteName
                                              FROM Table3,BaSequenceScheduleHdr,BAAnalyteSampleHdr
                                              WHERE Table3.nTable2No = BaSequenceScheduleHdr.nTable2No
                                                    AND Table3.cStatusIndi &lt;&gt; 'D'
                                       )
  
  SET @stmt = 'SELECT     
              Table1.nBaSequenceScheduleHdrNo,
              Table1.vSampleId,
              Table1.vSampleName,
              Table1.iSeqNo,
              Table1.vSampleType,
              Table2.vInjectionVolume,
              Table3.vAnalyteName,
              Table4.nConcentration,
              Table1.cStatusIndi
  
          FROM Table1
  
                  INNER JOIN BaSequenceScheduleHdr
                        ON(BaSequenceScheduleHdr.nBaSequenceScheduleHdrNo =  Table1.nBaSequenceScheduleHdrNo
                           AND BaSequenceScheduleHdr.cStatusIndi &lt;&gt; ''D'')
  
                  INNER JOIN Table2
                        ON(Table2.nTable2No = BaSequenceScheduleHdr.nTable2No
                           AND Table2.cStatusIndi &lt;&gt; ''D'')
  
                  INNER JOIN Table3
                        ON(Table3.nTable2No = BaSequenceScheduleHdr.nTable2No
                           AND Table3.cStatusIndi &lt;&gt; ''D'')          
  
                  LEFT JOIN BAAnalyteSampleHdr
                       ON(BAAnalyteSampleHdr.nTable2No = BaSequenceScheduleHdr.nTable2No
                          AND BAAnalyteSampleHdr.nTable3No = Table3.nTable3No
                          AND BAAnalyteSampleHdr.cStatusIndi &lt;&gt; ''D'')          
  
                  LEFT JOIN Table4
                       ON(Table4.nBAAnalyteSampleHdrNo = BAAnalyteSampleHdr.nBAAnalyteSampleHdrNo
                          AND Table4.vSampleID =  Table1.vSampleId
                          AND Table4.cStatusIndi &lt;&gt; ''D'')
  
          PIVOT (
                  SUM(Table4.nConcentration) FOR Table3.vAnalyteName IN ( ' + @cols + ' )
                ) AS PVT'
  
  exec(@stmt)
  </code></pre>
  
  <h2>Reason For Using Pivot in Procedure</h2>
  
  <p>Here <code>concentration</code> is dependent on <code>Analyte</code>. So what I want is that that depending on dynamic <code>Analyte</code> name its <code>concentration</code> should be under that column.</p>
  
  <h2>How it should be work</h2>
  
  <p>In here, first <code>@cols</code> will contain all the <code>analyte</code> names which will passed to the <strong><code>PIVOT</code></strong>. And the final <strong><code>exec(@stmt)</code></strong> should return the all data as a <strong><code>View</code></strong></p>
  
  <hr>
  
  <p>But I'm getting the error: <strong><code>(10 row(s) affected) Msg 102, Level 15, State 1, Line 37 Incorrect syntax near ')'.</code></strong></p>
  
  <p>And also, I'm using <strong>pivot</strong> first time. So Please let me know if any error in it also.</p>