title: Incorrect syntax near the keyword 'AS'. in sql sp execute
body: <p>I am working on this stored procedure and the database is complaining about a syntax error:</p>
  
  <pre><code>SET ANSI_NULLS ON;
  GO
  SET QUOTED_IDENTIFIER ON;
  GO
  ALTER PROCEDURE [dbo].[p_opd_insert_transaction_table]               
  --@lkp_Table   NVarchar(500) = ''               
  @Table_name  NVARCHAR(500),
  @fiscal_year VARCHAR(4)               
  --@FieldNameTranspose NVarchar(MAX) = ''                
  AS
      BEGIN
         SET NOCOUNT ON;
         DECLARE @colsUnpivot NVARCHAR(MAX), @query NVARCHAR(MAX);
         SET @colsUnPivot = STUFF(
                            (
                             SELECT ','+code_name
                             FROM opd_lkp_scholar t
                             WHERE fiscal_year = 2015
                              AND 
                              code_name 
                                NOT IN('UNITID', 'Institution', 'City', 'State', 'Zip')
                             FOR XML PATH(''), TYPE
                            ).value('.', 'NVARCHAR(MAX)'), 1, 1, '');
         PRINT @colsUnPivot;
         SET @query = 'SELECT unitid, institution, city, 
                          state, zip, code_name, lkp_value                 
                    FROM (SELECT '+@colsUnpivot+'             
                    FROM '+@Table_name+') AS cp                  
                    UNPIVOT (lkp_value for code_name  IN ('+
                     @colsUnPivot+')) AS up';
         PRINT @Query;
         EXEC (@query);
      END;               
  </code></pre>
  
  <p>When I execute this stored procedure, I get this error:</p>
  
  <blockquote>
    <p>Msg 156, Level 15, State 1, Line 4<br>
    Incorrect syntax near the keyword 'AS'.</p>
  </blockquote>
  
  <p>Can you please tell me what's wrong here?</p>