求助:存储过程调用存储过程如何返回错误?
在存储过程aaa中 调用存储过程bbb 如何在执行bbb时候返回错误。
我采用 bbb在执行成功后 output参数是 '返回成功!'来判断,不知道是否可行?
CREATE PROCEDURE aaa  
@dt datetime,     
@Tagid int,   
@msg as varchar(100)    output  
AS
Begin transaction if tagid=1
begin
--处理
..
..
..
--调用
declare @msg1 varchar(100)
exec bbb @dt,21,@msg1
set @msg=@msg1
If @msg<>'返回成功!' 
goto on_error
exec bbb @dt,22,@msg1
set @msg=@msg1
If @msg<>'返回成功!' 
goto on_error exec bbb @dt,23,@msg1
set @msg=@msg1
If @msg<>'返回成功!' 
goto on_error
...
endcommit
return 1on_error:
rollback
return -1
GOCREATE PROCEDURE bbb
@dt datetime,     
@Tagid int,   
@msg as varchar(100)    output  
AS
Begin transaction if tagid=21
begin
  set @msg='出错情况1'
  --处理过程  
  If @@error<>0 
    goto on_error
  SET @MSG='返回成功!' 
endif tagid=22
begin
  set @msg='出错情况2'
  --处理过程  
  If @@error<>0 
    goto on_error
  SET @MSG='返回成功!' 
endif tagid=23
begin
  set @msg='出错情况3'
  --处理过程  
  If @@error<>0 
    goto on_error
  SET @MSG='返回成功!' 
endcommit
return 1on_error:
rollback
return -1
GO

解决方案 »

  1.   

    ---有3种方法获得返回值 参考
    http://blog.csdn.net/fredrickhu/archive/2009/09/23/4584118.aspx
      

  2.   

    返回代码通常用在存储过程的流程中,可以为每种错误返回不同的代码,可以在t-sql后使用@@error函数来判断是否该语句的执行期间有错误发生用output可以,用return也行
      

  3.   

    可以的,通过@msg是否为执行成功来判断。
      

  4.   


    目前的问题是,
    我在存储过程中有一个
    IF exists (SELECT top 1 * from .....)
    begin
         SELECT TOP 1 @msg=字段+'不正确' FROM ....
         goto on_error
    end
    在存储过程在运行到这里,因为检查出来有错误数据,所以 goto on_error 并且@msg信息就是错误数据的地方。但是如何在存储过程aaa调用bbb的时候 把这个错误获取出来呢?
      

  5.   


    修改后的!CREATE PROCEDURE aaa  
    @dt datetime,     
    @Tagid int,   
    @msg as varchar(100)    output  
    AS
    Begin transaction if @Tagid=1
    begin
        --处理
        --调用
        declare @msg1 varchar(100)
        exec bbb @dt,21,@msg1 output
        set @msg=@msg1
        If @msg<>'返回成功!' 
            goto on_error
        exec bbb @dt,22,@msg1 output
        set @msg=@msg1
        If @msg<>'返回成功!' 
            goto on_error    exec bbb @dt,23,@msg1 output
        set @msg=@msg1
        If @msg<>'返回成功!' 
            goto on_errorendcommit
    return 1on_error:
    rollback
    return -1
    GOCREATE PROCEDURE bbb
    @dt datetime,     
    @Tagid int,   
    @msg as varchar(100)    output  
    AS
    Begin transaction if @Tagid=21
    begin
      set @msg='出错情况1'
      --处理过程  
      If @@error<>0 
        goto on_error
      SET @MSG='返回成功!' 
    endif @Tagid=22
    begin
      set @msg='出错情况2'
      --处理过程  
      If @@error<>0 
        goto on_error
      SET @MSG='返回成功!' 
    endif @Tagid=23
    begin
      set @msg='出错情况3'
      --处理过程  
      If @@error<>0 
        goto on_error
      SET @MSG='返回成功!' 
    endcommit
    return 1on_error:
    rollback
    return -1
    GO
    =============declare @msg varchar(100)
    set @msg = ''
    exec aaa '',1, @msg output 
    select @msg
      

  6.   

    CREATE PROCEDURE aaa  
    @dt datetime,     
    @Tagid int,   
    @msg as varchar(100)    output  
    AS
    Begin transaction if @Tagid=1
    begin
        --处理
        --调用
        declare @msg1 varchar(100)
        exec bbb @dt,21,@msg1 output
        set @msg=@msg1
        If @msg<>'返回成功!' 
            goto on_error
        exec bbb @dt,22,@msg1 output
        set @msg=@msg1
        If @msg<>'返回成功!' 
            goto on_error    exec bbb @dt,23,@msg1 output
        set @msg=@msg1
        If @msg<>'返回成功!' 
            goto on_errorendcommit
    return 1on_error:
    rollback
    return -1
    GOCREATE PROCEDURE bbb
    @dt datetime,     
    @Tagid int,   
    @msg as varchar(100)    output  
    AS
    Begin transaction if @Tagid=21
    begin
      set @msg='出错情况1'
      --处理过程  
      If @@error<>0 
        goto on_error
      SET @MSG='返回成功!' 
    endif @Tagid=22
    begin
      set @msg='出错情况2'
      --处理过程  
      If @@error<>0 
        goto on_error
      SET @MSG='返回成功!' 
    endif @Tagid=23
    begin
      set @msg='出错情况3'
      --处理过程  
      If @@error<>0 
        goto on_error
      SET @MSG='返回成功!' 
    endcommit
    return 1on_error:
    rollback
    return -1
    GO
      

  7.   

    --测试 
    declare @msg varchar(100)
    set @msg = ''
    exec aaa '',1, @msg output 
    select @msg
    =========
    返回成功3!alter PROCEDURE aaa  
    @dt datetime,     
    @Tagid int,   
    @msg as varchar(100)    output  
    AS
    Begin transaction if @Tagid=1
    begin
        --处理
        --调用
        declare @msg1 varchar(100)
         set @msg1 = ''
        exec bbb @dt,21,@msg1 output
        set @msg=@msg1
        If @msg<>'返回成功1!' 
            goto on_error
        exec bbb @dt,22,@msg1 output
        set @msg=@msg1
        If @msg<>'返回成功2!' 
            goto on_error    exec bbb @dt,23,@msg1 output
        set @msg=@msg1
        If @msg<>'返回成功3!' 
            goto on_errorendcommit
    return 1on_error:
    rollback
    return -1
    GOalter PROCEDURE bbb
    @dt datetime,     
    @Tagid int,   
    @msg as varchar(100)    output  
    AS
    Begin transaction if @Tagid=21
    begin
      set @msg='出错情况1'
      --处理过程  
      If @@error<>0 
        goto on_error
      SET @MSG='返回成功1!' 
    endif @Tagid=22
    begin
      set @msg='出错情况2'
      --处理过程  
      If @@error<>0 
        goto on_error
      SET @MSG='返回成功2!' 
    endif @Tagid=23
    begin
      set @msg='出错情况3'
      --处理过程  
      If @@error<>0 
        goto on_error
      SET @MSG='返回成功3!' 
    endcommit
    return 1on_error:
    rollback
    return -1
    GO
      

  8.   

    同意楼上的
    SQL2005以后改成TRY CATCH获取错误了,有几个系统函数处理错误信息。
      

  9.   

    解决了,是我有一个地方写错了IF exists (SELECT top 1 * from ..... 条件=0 ) 
    begin 
        SELECT TOP 1 @msg=字段+'不正确' FROM .... 条件<>0
        goto on_error 
    end 条件那里写错了,找到错误,但是赋值时条件又换成另外一个了 导致 @msg=null
      

  10.   

    RAISERROR (N'This is message %s %d.', -- Message text.
               10, -- Severity,
               1, -- State,
               N'number', -- First argument.
               5); -- Second argument.
    -- The message text returned is: This is message number 5.
    GO