关键字“AS”附近的语法不正确。在sql sp执行

时间:2016-10-11 20:29:25

标签: sql-server tsql stored-procedures

我正在处理这个存储过程,数据库抱怨语法错误:

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;               

执行此存储过程时,出现此错误:

  

Msg 156,Level 15,State 1,Line 4
  关键字“AS”附近的语法不正确。

你能告诉我这里有什么问题吗?

0 个答案:

没有答案