首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请帮忙看看下面存储过程,有什么语法异常

2014-01-01 
请大虾帮忙看看下面存储过程,有什么语法错误Create Procedure SP_BindCoupons(@actID INT, --活动ID@cardN

请大虾帮忙看看下面存储过程,有什么语法错误
Create Procedure SP_BindCoupons(
@actID INT, --活动ID
@cardNo int, --卡号
@count10 int, --生成的10元券数量
@count20 int, --生成的10元券数量
@count30 int --生成的10元券数量
)
AS
DECLARE @winCount10 int,
        @winCount20 int,
        @winCount30 int,
        @nextWinNo int,
        @tempValue int,
        @errorValue nvarchar(100),
        @errorCode int
    Begin
           Set NOCOUNT ON;
           Set XACT_ABORT ON;
           begin tran bindCoupon  --开始事务bindCoupon 
           --查询该活动可生成的10元券数量
select @winCount10=(wc.WinningEnd-wc.NextWinningNo),@nextWinNo=wc.NextWinningNo from Winning_Config wc where wc.ID=@actID and wc.WinningType=10
Set @tempValue=1
if(@winCount10>=@count10)
begin 
--绑定10元券
while(@tempValue<=@count10)
begin
INSERT INTO [Winning_List]([ActivityID],[WinningNo],[WinningType],[AccID],[Status],[CardNo],[UseDate]
,[Cdate]
,[UseType])
 VALUES
(@actID,@nextWinNo+@tempValue,10,'',0,@cardNo,'1900-01-01 00:00:00.000',getdate(),null)

Set @tempValue=@tempValue+1

end
--修改代金券的下一个券号
UPDATE [Winning_Config] SET [NextWinningNo] = @nextWinNo+@winCount10 WHERE ID=@actID
end
else
begin
Set @errorValue+='该活动10元券数量不足;'
Set @errorCode=1
end 
end   

--查询该活动可生成的20元券数量
select @winCount20=(wc.WinningEnd-wc.NextWinningNo),@nextWinNo=wc.NextWinningNo from Winning_Config wc where wc.ID=@actID and wc.WinningType=20
Set @tempValue=1
if(@winCount20>=@count20)
begin 
--绑定20元券
while(@tempValue<=@count20)
begin
INSERT INTO [Winning_List]([ActivityID],[WinningNo],[WinningType],[AccID],[Status],[CardNo],[UseDate]
,[Cdate]
,[UseType])
 VALUES
(@actID,@nextWinNo+@tempValue,20,'',0,@cardNo,'1900-01-01 00:00:00.000',getdate(),null)

Set @tempValue=@tempValue+1
end
--修改代金券的下一个券号
UPDATE [Winning_Config] SET [NextWinningNo] = @nextWinNo+@winCount20 WHERE ID=@actID
end
else
begin
Set @errorValue+='该活动20元券数量不足;'
Set @errorCode=1
end 
end   
 
            
--查询该活动可生成的30元券数量
select @winCount30=(wc.WinningEnd-wc.NextWinningNo),@nextWinNo=wc.NextWinningNo from Winning_Config wc where wc.ID=@actID and wc.WinningType=30
Set @tempValue=1
if(@winCount30>=@count30)
begin 
--绑定30元券
while(@tempValue<=@count30)
begin
INSERT INTO [Winning_List]([ActivityID],[WinningNo],[WinningType],[AccID], [Status],[CardNo],[UseDate]
,[Cdate]
,[UseType])
 VALUES
(@actID,@nextWinNo+@tempValue,30,'',0,@cardNo,'1900-01-01 00:00:00.000',getdate(),null)

Set @tempValue=@tempValue+1
end
--修改代金券的下一个券号
UPDATE [Winning_Config] SET [NextWinningNo] = @nextWinNo+@winCount30 WHERE ID=@actID
end
else
begin
Set @errorValue+='该活动30元券数量不足;'
Set @errorCode=1
end 
end 

           if @@error<>0  --判断是否出错   
               begin --如果出错  
                    rollback   tran  bindCoupon  -- 回滚事务到bindCoupon 的还原点
                    commit   tran  bindCoupon 
               end 
           else  --没有出错               
               commit  tran  bindCoupon 


[解决办法]
少了很多begin,总的来说没配对好
[解决办法]

CREATE PROCEDURE SP_BindCoupons
    (
      @actID INT , --活动ID
      @cardNo INT , --卡号
      @count10 INT , --生成的10元券数量
      @count20 INT , --生成的10元券数量
      @count30 INT --生成的10元券数量
    )
AS
    DECLARE @winCount10 INT ,
        @winCount20 INT ,
        @winCount30 INT ,
        @nextWinNo INT ,
        @tempValue INT ,
        @errorValue NVARCHAR(100) ,
        @errorCode INT
    BEGIN
        SET NOCOUNT ON;
        SET XACT_ABORT ON;
        BEGIN TRAN bindCoupon  --开始事务bindCoupon 
           --查询该活动可生成的10元券数量
        SELECT  @winCount10 = ( wc.WinningEnd - wc.NextWinningNo ) ,
                @nextWinNo = wc.NextWinningNo
        FROM    Winning_Config wc
        WHERE   wc.ID = @actID
                AND wc.WinningType = 10
        SET @tempValue = 1
        IF ( @winCount10 >= @count10 )
            BEGIN 
--绑定10元券
                WHILE ( @tempValue <= @count10 )
                    BEGIN
                        INSERT  INTO [Winning_List]
                                ( [ActivityID] ,
                                  [WinningNo] ,
                                  [WinningType] ,
                                  [AccID] ,
                                  [Status] ,
                                  [CardNo] ,
                                  [UseDate] ,
                                  [Cdate] ,
                                  [UseType]
                                )
                        VALUES  ( @actID ,
                                  @nextWinNo + @tempValue ,


                                  10 ,
                                  '' ,
                                  0 ,
                                  @cardNo ,
                                  '1900-01-01 00:00:00.000' ,
                                  GETDATE() ,
                                  NULL
                                )

                        SET @tempValue = @tempValue + 1

                    END
--修改代金券的下一个券号
                UPDATE  [Winning_Config]
                SET     [NextWinningNo] = @nextWinNo + @winCount10
                WHERE   ID = @actID
            END
        ELSE
            BEGIN
                SET @errorValue += '该活动10元券数量不足;'
                SET @errorCode = 1
            END  
    END   

--查询该活动可生成的20元券数量
    SELECT  @winCount20 = ( wc.WinningEnd - wc.NextWinningNo ) ,
            @nextWinNo = wc.NextWinningNo
    FROM    Winning_Config wc
    WHERE   wc.ID = @actID
            AND wc.WinningType = 20
    SET @tempValue = 1
    IF ( @winCount20 >= @count20 )
        BEGIN 
--绑定20元券
            WHILE ( @tempValue <= @count20 )
                BEGIN
                    INSERT  INTO [Winning_List]
                            ( [ActivityID] ,
                              [WinningNo] ,
                              [WinningType] ,
                              [AccID] ,
                              [Status] ,
                              [CardNo] ,
                              [UseDate] ,
                              [Cdate] ,
                              [UseType]


                            )
                    VALUES  ( @actID ,
                              @nextWinNo + @tempValue ,
                              20 ,
                              '' ,
                              0 ,
                              @cardNo ,
                              '1900-01-01 00:00:00.000' ,
                              GETDATE() ,
                              NULL
                            )

                    SET @tempValue = @tempValue + 1
                END
--修改代金券的下一个券号
            UPDATE  [Winning_Config]
            SET     [NextWinningNo] = @nextWinNo + @winCount20
            WHERE   ID = @actID
        END
    ELSE
        BEGIN
            SET @errorValue += '该活动20元券数量不足;'
            SET @errorCode = 1
        END  

--查询该活动可生成的30元券数量
    SELECT  @winCount30 = ( wc.WinningEnd - wc.NextWinningNo ) ,
            @nextWinNo = wc.NextWinningNo
    FROM    Winning_Config wc
    WHERE   wc.ID = @actID
            AND wc.WinningType = 30
    SET @tempValue = 1
    IF ( @winCount30 >= @count30 )
        BEGIN 
--绑定30元券
            WHILE ( @tempValue <= @count30 )
                BEGIN
                    INSERT  INTO [Winning_List]
                            ( [ActivityID] ,
                              [WinningNo] ,
                              [WinningType] ,
                              [AccID] ,
                              [Status] ,
                              [CardNo] ,
                              [UseDate] ,
                              [Cdate] ,


                              [UseType]
                            )
                    VALUES  ( @actID ,
                              @nextWinNo + @tempValue ,
                              30 ,
                              '' ,
                              0 ,
                              @cardNo ,
                              '1900-01-01 00:00:00.000' ,
                              GETDATE() ,
                              NULL
                            )

                    SET @tempValue = @tempValue + 1
                END
--修改代金券的下一个券号
            UPDATE  [Winning_Config]
            SET     [NextWinningNo] = @nextWinNo + @winCount30
            WHERE   ID = @actID
        END
    ELSE
        BEGIN
            SET @errorValue += '该活动30元券数量不足;'
            SET @errorCode = 1
        END  


    IF @@error <> 0  --判断是否出错   
        BEGIN --如果出错  
            ROLLBACK   TRAN  bindCoupon  -- 回滚事务到bindCoupon 的还原点
            COMMIT   TRAN  bindCoupon 
        END 
    ELSE  --没有出错               
        COMMIT  TRAN  bindCoupon 


 


[解决办法]
呵呵,我最近上班都没事,手都给你改了下,貌似中间多两个end,最后少了一个end,你试试:

Create Procedure SP_BindCoupons(
@actID INT, --活动ID
@cardNo int, --卡号
@count10 int, --生成的10元券数量
@count20 int, --生成的10元券数量
@count30 int --生成的10元券数量
)
AS
DECLARE @winCount10 int,
@winCount20 int,
    @winCount30 int,
    @nextWinNo int,
    @tempValue int,
    @errorValue nvarchar(100),
    @errorCode int
Begin
Set NOCOUNT ON;
Set XACT_ABORT ON;
begin tran bindCoupon  --开始事务bindCoupon 
--查询该活动可生成的10元券数量
select @winCount10=(wc.WinningEnd-wc.NextWinningNo),@nextWinNo=wc.NextWinningNo from Winning_Config wc where wc.ID=@actID and wc.WinningType=10
Set @tempValue=1
if(@winCount10>=@count10)
begin 
--绑定10元券
while(@tempValue<=@count10)
begin
INSERT INTO [Winning_List]([ActivityID],[WinningNo],[WinningType],[AccID],[Status],[CardNo],[UseDate]
,[Cdate]
,[UseType])
VALUES
(@actID,@nextWinNo+@tempValue,10,'',0,@cardNo,'1900-01-01 00:00:00.000',getdate(),null)

Set @tempValue=@tempValue+1 

end
--修改代金券的下一个券号
UPDATE [Winning_Config] SET [NextWinningNo] = @nextWinNo+@winCount10 WHERE ID=@actID


end
else
begin
Set @errorValue+='该活动10元券数量不足;' 
Set @errorCode=1 
end  

--查询该活动可生成的20元券数量
select @winCount20=(wc.WinningEnd-wc.NextWinningNo),@nextWinNo=wc.NextWinningNo from Winning_Config wc where wc.ID=@actID and wc.WinningType=20 
Set @tempValue=1
if(@winCount20>=@count20)
begin 
--绑定20元券
while(@tempValue<=@count20)
begin
INSERT INTO [Winning_List]([ActivityID],[WinningNo],[WinningType],[AccID],[Status],[CardNo],[UseDate]
,[Cdate]
,[UseType])
VALUES
(@actID,@nextWinNo+@tempValue,20,'',0,@cardNo,'1900-01-01 00:00:00.000',getdate(),null)

Set @tempValue=@tempValue+1 
end
--修改代金券的下一个券号
UPDATE [Winning_Config] SET [NextWinningNo] = @nextWinNo+@winCount20 WHERE ID=@actID
end
else
begin
Set @errorValue+='该活动20元券数量不足;' 
Set @errorCode=1 
end  
             
--查询该活动可生成的30元券数量
select @winCount30=(wc.WinningEnd-wc.NextWinningNo),@nextWinNo=wc.NextWinningNo from Winning_Config wc where wc.ID=@actID and wc.WinningType=30 
Set @tempValue=1
if(@winCount30>=@count30)
begin 
--绑定30元券
while(@tempValue<=@count30)
begin
INSERT INTO [Winning_List]([ActivityID],[WinningNo],[WinningType],[AccID], [Status],[CardNo],[UseDate]
,[Cdate]
,[UseType])
VALUES
(@actID,@nextWinNo+@tempValue,30,'',0,@cardNo,'1900-01-01 00:00:00.000',getdate(),null)

Set @tempValue=@tempValue+1 
end
--修改代金券的下一个券号
UPDATE [Winning_Config] SET [NextWinningNo] = @nextWinNo+@winCount30 WHERE ID=@actID
end
else
begin
Set @errorValue+='该活动30元券数量不足;' 
Set @errorCode=1 
end  

    if @@error<>0  --判断是否出错   
begin --如果出错  
rollback   tran  bindCoupon  -- 回滚事务到bindCoupon 的还原点
commit   tran  bindCoupon 
end 
    else  --没有出错               
commit  tran  bindCoupon  
end



我在我电脑上可以了,结果如下:
请帮忙看看下面存储过程,有什么语法异常

热点排行