必须在存储过程中声明标量变量“@bit”作为输出参数

时间:2013-04-02 15:51:20

标签: sql sql-server database tsql

我有这个程序,但是当它完美时,没有两个if设置输出参数@bit。当我尝试执行proc p3时,发生以下错误

Must declare the scalar variable "@bit".

这是代码

create proc p3 @codP int, @codM int, @dela datetime, @panala datetime, @pret smallint, @bit bit output
as
begin
    declare @val smallint
    set @val = (select COUNT(*)
                from OferteSpeciale as OS
                where OS.codP = @codP and OS.codM = @codM and 
                        ((@panala >= OS.dela and @panala <= OS.panala)
                        or (@dela >= OS.dela and @dela <= OS.panala) or
                        (@dela <= OS.dela and @panala >= OS.panala)))
    print @val
    if (@val = 0)
        begin
            insert into OferteSpeciale (codP,codM,dela,panala,pret) values (@codP,@codM,@dela,@panala,@pret)
            declare @others smallint
            set @others = (select COUNT(*)
                         from Cataloage as c, OferteSpeciale as os
                         where ((c.codM = @codM and c.codP = @codP and c.pret < @pret) or
                            (os.codM = @codM and os.codP = @codP and os.pret < @pret)))
            if (@others > 0)
              set @bit = 0
            else
              set @bit = 1
        end
    else
      RAISERROR 50001 'There is already a special offer for that product, in that shop, between that dates'
end
declare @bit bit
exec p3 1, 3, '2013-04-15', '2013-04-15', 25, @bit output

1 个答案:

答案 0 :(得分:2)

就像快速回答一样,在声明存储过程之后,你错过了一个“GO”,如果我按照你提供的那样尝试运行它,如果我添加一个GO来分离声明,我会得到一个不同的错误消失执行。

end
GO
declare @bit bit
exec p3 1, 3, '2013-04-15', '2013-04-15', 25, @bit output

显然我必须删除存储过程的内容,因为我的数据库中没有这些对象,但问题似乎是围绕“@Bit”