存储过程内的')'附近的语法不正确

时间:2013-11-06 12:24:06

标签: sql-server sql-server-2008 tsql dynamic-sql

我在尝试运行下面的动态sql时,当前收到以下错误;有什么我错过了导致这个错误被抛出的东西吗?....

  

Msg 102,Level 15,State 1,Procedure calc,Line 32语法不正确   靠近')'。

    CREATE PROCEDURE calc(@pC INT, @Array1 varchar(500) ,@pT VARCHAR(15), @pTN VARCHAR(15), @pPK BIGINT, @pCI FLOAT, @pFK BIGINT)
        AS 
        BEGIN
        --calculate peer statistics for level of peer group   
        DECLARE @sMI FLOAT, @num25 FLOAT, @num75 FLOAT, @num25V FLOAT, @num75V FLOAT
        DECLARE @sMD FLOAT, @sME FLOAT, @sIQ FLOAT, @sSD FLOAT, @sMin FLOAT, @sMax FLOAT, @sDiff FLOAT
        DECLARE @sLK BIGINT, @sNL BIGINT, @sNI BIGINT
        DECLARE @sUNC NVARCHAR(5)
        DECLARE @SQL NVARCHAR(MAX)


SET @SQL =  'IF ('+@pFK+' = 0) 
                BEGIN
                    INSERT INTO '+@pTN+' (fldBA) VALUES (2); 
                END
            ELSE  
                BEGIN  
                    IF ((SELECT COUNT(*) FROM '+@pTN+' WHERE fldFPK = '+@pFK+' and fldPK='+@pPK+' and fldCIA='+@pCI+') > 0)
                        BEGIN           
                            UPDATE '+@pTN+' SET fldAVA = 0, fldPK = '+@pPK+', fldDTA = GETDATE(), fldCIA = '+@pCI+',
                            fldME = '+@sME+',fldMD = '+@sMD+',fldIQ = '+@sIQ+',fldSD = '+@sSD+', fldND = '+@pC+' + 1,
                            fldNI = '+@sNI+', fldNL = '+@sNL+', fldMin = '+@sMin+', fldMax = '+@sMax+',fldUNC = '+@sUNC+'
                            WHERE fldFPK = '+@pFK+' and fldPK = '+@pPK+' and fldCIAc = '+@pCI+'
                        END
                    ELSE
                        BEGIN               
                            INSERT INTO '+@pTN+' (fldFPK, fldAVA, fldPK, fldDTA, fldCIA,fldMean,fldMD,fldIQ,fldSD,fldND,fldNI,fldNL,fldMin,fldMax,fldUNC) VALUES('+@pFK+', 0, '+@pPK+', GETDATE(), 
                            '+@pCI+', '+@sME+', '+@sMD+','+@sIQ+','+@sSD+','+@pC+'+ 1, '+@sNI+', '+@sNL+', '+@sMin+', '+@sMax+', '+@sUNC+')
                        END
                END'

exec @SQL

1 个答案:

答案 0 :(得分:3)

我在SQL Server Management Studio中复制并粘贴了该过程。您没有任何语法错误。 您只需使用CREATE PROCEDURE关闭语句END,因为您使用BEGIN启动了该语句。你可以这样做:

CREATE PROCEDURE calc(@pC INT, @Array1 varchar(500) ,@pT VARCHAR(15), @pTN VARCHAR(15), @pPK BIGINT, @pCI FLOAT, @pFK BIGINT)
    AS 
    BEGIN

    /* BODY OF PROCEDURE */

    SET @SQL = ''

    EXEC @SQL

    END