USE [ykbz]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[helloo] 
@num int=0 output
AS
if(1=1)
begin
while(@num<10)
begin
set @num=@num+1
print @num
end
end
declare @num1 int
exec helloo @num1 OUTPUT
因为要测试另外一个存储过程,我就写了这个简单点的存储过程,调试时却不执行,不知道为什么

解决方案 »

  1.   

    CREATE PROCEDURE [dbo].[helloo] 
    @num int output 
    AS 
    SET @num=0
    if(1=1) 
    begin 
    while(@num <10) 
    begin 
    set @num=@num+1 
    print @num 
    end 
    end 
    godeclare @num1 int 
    exec helloo @num1 OUTPUT 
    SELECT @NUM1DROP PROC HELLOO
      

  2.   

    过程内要初始化@num的值
    ...
    set @num=0
    if(1=1) 
    ...
      

  3.   

    ALTER PROCEDURE [dbo].[helloo] 
    @num int,@num1 int=0 output 
    AS 
    begin 
    while(@num <10) 
    begin 
    set @num=@num+1 
    end 
    select @num1 = @num
    end declare @num1 int 
    exec helloo 8,@num1 OUTPUT 
    select @num1
      

  4.   

    最后一句,要加判断,否则无限递归了,而sql 函数,proc等,递归最多32层
      

  5.   

    我初始化了 也是不行的 本来准备要加上 set @num=0 我又给去掉了
      

  6.   

    参数的默认值是指在不传(不写参数)的时候才默认,而你此时给@num传了null,所以@num的初始值不会为0,要在过程内初始化,看下面的例子:
    ALTER   PROCEDURE [dbo].[helloo]
    @num INT OUTPUT,@b INT= 20
    AS
    SET @num=0
    IF (1 = 1)
    BEGIN
        WHILE (@num < 10)
        BEGIN
            SET @num = @num + 1 
            PRINT @num
        END
    END

    SET @num=@num+@b 
    go

    DECLARE @s INT 
    DECLARE @num1 INT EXEC helloo @num1 OUTPUT
    SELECT @num1
    --result
    /*
    30
    */EXEC helloo @num1 OUTPUT,@s
    SELECT @num1
    /*
    null
    */
      

  7.   


    Alter PROCEDURE [dbo].[helloo] 
    @num int=0 output 
    AS 
    SET @num=ISNULL(@num,0)
    if(1=1) 
    begin 
    while(@num <10) 
    begin 
    set @num=@num+1 
    print @num 
    end 
    end 
    GOdeclare @num1 int 
    exec helloo @num1 OUTPUT --the result--
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10