@@ERROR需要在每句SQL后检测2005/2008可以用Try-Catch

解决方案 »

  1.   

    declare @a  int--执行存储过程
    exec @a = my_proc--出错
    if @a <> 0
    print 'error'
      

  2.   

    create proc pa
    as
    begin
    declare @err int
    exec pb @err output
    select @err
    end
    gocreate proc pb
    @err int output
    as
    begin
    select 'hoho'
    set @err=@@error
    end
    go
    --测试:
    exec pa
    /*-----------
    hoho(1 row(s) affected)
    -----------
    0(1 row(s) affected)
    */
      

  3.   

    RAISERROR
    返回用户定义的错误信息并设系统标志,记录发生错误。通过使用 RAISERROR 语句,客户端可以从 sysmessages 表中检索条目,或者使用用户指定的严重度和状态信息动态地生成一条消息。这条消息在定义后就作为服务器错误信息返回给客户端。