大家好怎样设置一个存储过程使其中任何一个批处理语句出错就执行另一个存储过程(记录日志)然后结束存储过程
注:存储过程有5000行代码,上百个批处理语句
    不能每个批处理后面都加一个判断@@ERROR<>0如:
create proc aa
as
begin
p1
select ...
into  ...p2
...
....
.
.set xact_abort on
begin tran
p100
...
...
commit tran
endif (@@ERROR=0)
exec test.test.putlog 'succeed'
else
exec test.test.putlog 'failure'

解决方案 »

  1.   

    2005可以用try catch
    2000的话就存储过程中调用存储过程,应该是这样吧~
      

  2.   

    create proc ***
    as
    begin
          exec 被调用proc
          if (@@error <> 0)
          ......
    end
      

  3.   

    create proc aa 
    as 
    begin 
    --p1 (批处理语句1)
    select ... 
    into  ... --p2 (批处理语句2)
    ... 
    ... . 

    . set xact_abort on 
    begin tran 
    --p100 (批处理语句100)
    ... 
    ... 
    commit tran 
    end 参考:
    if (@@ERROR=0) 
    exec test.test.putlog 'succeed' 
    else 
    exec test.test.putlog 'failure' 我的意思是在这100个批处理语句中
    任何一个执行出错
    都结束存储过程
    并将执行结果写入日志表(调用另外一个存储过程)
      

  4.   

    create proc test
    as批处理1
    if( @error<>0)
       goto ErrFlag批处理2
    if()
    ...return 1
    ErrFlag:
       exec procShowErrMessage
      

  5.   

    [Quote=引用楼主 GDC_ZhaoYZ0304360 的帖子:]注:存储过程有5000行代码,上百个批处理语句 
        不能每个批处理后面都加一个判断@@ERROR <>0 /Quote]
      

  6.   

    sql server2005里已经引入了捕捉错误信息的模块
    ...
    begin try
       要捕捉错误的代码
    end trybegin catch
       如果捕捉到错误,则执行你想要做的事情
    end catch--
    这个很符合我们的习惯如果是在2000,只能是判断@@error,如果为1,则产生错误,则进行有错误的处理,否则继续执行即可