0

我目前正在开展一个项目,该项目涉及使用实体框架中的存储过程生成序列。我在这里使用 Code Project 文章,在尝试生成以下存储过程时,我收到了一个语法错误:

SET QUOTED_IDENTIFIER OFF

GO 
SET ANSI_NULLS OFF
GO 

CREATE PROCEDURE DBO.p_GetNextInSequence 
    @SequenceKey VARCHAR(20)
AS 
BEGIN 
SET NOCOUNT ON 
DECLARE @LASTNUMBER INT
DECLARE @NEXTNUMBER INT
DECLARE @FORMATTEDNUMBER VARCHAR(40)
DECLARE @MYKEY VARCHAR(30)

IF NOT EXISTS(SELECT 'X' 
              FROM SequenceControl 
              WHERE SequenceControl.SequenceKey = @SequenceKey )
BEGIN 
    SELECT    @SequenceKey AS SequenceKey, 
              CAST('' AS VARCHAR(30)) AS NextSequenceFormatted, 
              CAST(0 AS INT) AS NextSequenceInt 
    RETURN 
END 

DECLARE @SEQFORMAT VARCHAR(30), @IncrementBy INT, 
                   @IncrementStep INT, @zeropadtodigits INT

SELECT @SEQFORMAT = RTRIM(LTRIM(SequenceFormat)),
       @IncrementBy = IncrementBy, 
       @zeropadtodigits = ZeroPadToDigits 
FROM    SequenceControl 
WHERE    SequenceControl.SequenceKey = @SequenceKey 

DECLARE @THEROWCOUNT INT 
SET @THEROWCOUNT    = 0 

BEGIN TRANSACTION T1
    WHILE ( @THEROWCOUNT =0 ) 
    BEGIN 
        SELECT   @LASTNUMBER= LastSequence 
        FROM     SequenceControl
        WHERE    SequenceControl.SequenceKey= @SequenceKey 

        UPDATE     SequenceControl 
        SET        LastSequence = @LASTNUMBER + @IncrementBy 
        WHERE      SequenceKey = @SequenceKey AND    
                   LastSequence = @LASTNUMBER 

        SELECT @THEROWCOUNT = @@ROWCOUNT
    END 
COMMIT TRANSACTION T1 

--SQL Complains near this region...
DECLARE @FMTNUM VARCHAR(20) 
SET @NEXTNUMBER = @LASTNUMBER + @IncrementBy 
IF ( @zeropadtodigits>0) 
    SET @FMTNUM = ( REPLICATE('0', @zeropadtodigits) + 
                    CAST(@NEXTNUMBER AS VARCHAR(20)), @zeropadtodigits) 
ELSE 
    SET @FMTNUM = CAST(@NEXTNUMBER AS VARCHAR(10)) 


SET @FORMATTEDNUMBER = REPLACE(@SEQFORMAT,'[#]', @FMTNUM ) 

SELECT    @SequenceKey AS SequenceKey, 
          CAST(@FORMATTEDNUMBER AS VARCHAR(30)) AS NextSequenceFormatted, 
          @NEXTNUMBER AS NextSequenceInt 
END 

GO 
SET QUOTED_IDENTIFIER OFF
GO 
SET ANSI_NULLS ON    
GO

','SQL在第 56 行附近抱怨语法不正确:

DECLARE @FMTNUM VARCHAR(20) 

但是 - 我似乎无法弄清楚导致问题的原因或原因。此外,如果有人对在实体框架中使用存储过程(并返回输出)有任何建议或文章,我将不胜感激。

4

2 回答 2

3

实际错误在这里:

IF ( @zeropadtodigits>0) 
    SET @FMTNUM = ( REPLICATE('0', @zeropadtodigits) + 
                    CAST(@NEXTNUMBER AS VARCHAR(20)), @zeropadtodigits) 

更具体地说,在这里:

, @zeropadtodigits)

那个逗号没有任何作用,@zeropadtodigits也不适用于任何东西。括号内没有使用它作为参数的函数。

于 2012-04-18T16:17:14.973 回答
1

语法实际上在以下位置爆炸:

SET @FMTNUM = ( REPLICATE('0', @zeropadtodigits) + 
                CAST(@NEXTNUMBER AS VARCHAR(20)), @zeropadtodigits) 

为什么它在另一条线抱怨,我不知道。

于 2012-04-18T16:24:29.960 回答