如果让系统捕获到错误后不中止执行而由我自己指定是否退出?
例如
create procedure test (
@ErrMsg char(100)
)
as
   declare @a int
   declare @b int
   begin tran
   select @a=fieldA,@b=fieldB from tab_a
   insert into tab_b values (@a,@b)
   if @@errcode <> 0 
   begin
      rollback tran
      set @ErrMsg='Insert tab_b failed'
      return
   end
   ........
   commit tran
在上面的例子中,tab_b的字段fieldB不能为null,假如@b为空,则系统会拒绝执行并退出,那后面的if @@errcode <>0 就执行不了了,我想问的是有没办法让程序继续执行,由我的代码判断@errcode的值来决定是否退出?
在select @a=fieldA,@b=fieldB from tab_a执行后判断变量是否null的方法先不考虑,

解决方案 »

  1.   

    @@ERROR
    返回最后执行的 Transact-SQL 语句的错误代码。语法
    @@ERROR返回类型
    integer注释
    当 Microsoft&reg; SQL Server&#8482; 完成 Transact-SQL 语句的执行时,如果语句执行成功,则 @@ERROR 设置为 0。若出现一个错误,则返回一条错误信息。@@ERROR 返回此错误信息代码,直到另一条 Transact-SQL 语句被执行。您可以在 sysmessages 系统表中查看与 @@ERROR 错误代码相关的文本信息。由于 @@ERROR 在每一条语句执行后被清除并且重置,应在语句验证后立即检查它,或将其保存到一个局部变量中以备事后查看。
      

  2.   

    。先谢谢楼上的2位
    不过不是我要的答案,sql的帮助我看过了,我要的结果是如果让代码执行到if @@errcode <> 0 
    这里
      

  3.   

    晕,不好意思,if @@errcode <> 0 应该是 if @@error <> 0
    平时写@errcode写顺手了
      

  4.   

    tab_b的字段fieldB不能为null,假如@b为空,则系统会拒绝执行并退出,那后面的if @@errcode <>0 就执行不了了--应该不会吧
      

  5.   

    create table test (aa int not null ,bb int not null)
    declare @a int
    declare @b intset @a=1
    set @b=nullinsert into test (aa,bb) values (@a,@b)
    if @@error <> 0 
    print 'error!!'
    ----------------------------------------------------------------
    服务器: 消息 515,级别 16,状态 2,行 7
    无法将 NULL 值插入列 'bb',表 'PCGH.dbo.test';该列不允许空值。INSERT 失败。
    语句已终止。
    error!!
    这是在查询分析器里执行的,能判断@@error
    但我在前台程序里想通过出参获取自定义的错误描述,就不行,得到的错误描述总是系统的
    我想知道怎么弄,呵呵~~
      

  6.   

    很简单了...
    语句已终止。
    error!!   //这儿已经能说明if @@error 已经招待了,呵呵
    这是在查询分析器里执行的,能判断@@error
    /...问题的关键是你的存储过程少了两句话,下面是正确代码create procedure test (
    @ErrMsg char(100)
    )
    as
       set nocount on--新增加的
       declare @a int
       declare @b int
       begin tran
       select @a=fieldA,@b=fieldB from tab_a
       insert into tab_b values (@a,@b)
       if @@errcode <> 0 
       begin
          rollback tran
          set @ErrMsg='Insert tab_b failed'
          return
       end
       ........
       commit transet nocount off--新增加的
      

  7.   


    begin try 
    --输入你的DML
    end try
    begin catch
    --这里可以代替你刚才的if @@errcode <> 0 
    --输入要做的判断
    end catch这样就可以了
      

  8.   

    http://www.csdn.net/develop/article/18/18037.shtm标题     全接触SQLServer异常与孤立事务!    pengdali(原作)   
    关键字     SQLServer、异常、孤立事务 
      一、首先从SQLServer中Error讲起,SQL中错误处理有些怪辟 错误级别同是16但结果都不同。  select * from 一个不在的表
      if @@error<>0
        print '这个没有输出'
      go  raiserror('',16,3)
      if @@error<>0
        print '这个输出了'
      go  exec('select * from 一个不在的表')
      if @@error<>0
        print '这个输出了'
      go  exec sp_executesql N'select * from 一个不在的表'
      if @@error<>0
        print '这个输出了'这样你可以发现通过exec或sp_executesql执行可疑的sql,这样就可以在后面捕捉到被异常终止的错误。
    二、引出孤立事务:
      1、孤立事务的产生    select @@trancount 当前连接的活动事务数 --当前连接的活动事务数为0    begin tran    select * from 一个不在的表
        if @@error<>0
        begin
          print '没有执行到这里来!'
          if @@trancount<>0 rollback tran
        end    commit tran    select @@trancount 当前连接的活动事务数  --执行后你看看 当前连接的活动事务数为1,且重复执行会每次累加,这是很耗资源的。  应为rollback根本就没有被回滚。  2、使用现有手段解决孤立事务
        print @@trancount print '当前连接的活动事务数' --当前连接的活动事务数为0    if @@trancount<>0 rollback tran --在这里写可以让孤立事务只保持到下次你的过程被调用
        begin tran    select * from 一个不在的表
        if @@error<>0
        begin
          print '没有执行到这里来!'
          if @@trancount<>0 rollback tran
        end    commit tran    ---执行后你看看 当前连接的活动事务数为1,但重复执行不会累加
        print @@trancount print '当前连接的活动事务数'三、使用 set xact_abort 来控制部分违反约束的错误的执行过程  create table Table1 (a int check(a>100))
      go  set xact_abort on
      begin tran
        insert table1 values(10)
        print '这里没有被执行'
      commit tran
      go
      
      print '' print '==============================================' print ''
      
      set xact_abort off
      begin tran
        insert table1 values(10)
        print '这里被执行'
      commit tran  go
      drop table table1但 set xact_abort 对于编译产生的错误确没有起作用,且同样会产生孤立事务  set xact_abort on
      begin tran
        insert  一个不在的表 values(10)
        print '这里没有被执行'
      commit tran
      go  print '' print '==============================================' print ''  set xact_abort off
      begin tran
        insert  一个不在的表 values(10)
        print '这里没有被执行'
      commit tran
      go  select @@trancount 当前连接的活动事务数 ---有两个孤立事务
      if @@trancount<>0 rollback tran
    对于sql中怪辟的各种错误,和孤立事务在t-sql编程中一定要注意,小心孤立事务的陷阱,尽量避免浪费或孤立资源,Microsoft公开宣布过SQLServe下一版本Yukon将有内置异常处理语法。那时可以通过代码对无法预料的错误有更好的控制。