create procedure myProc( @no char(20) )
as
DECLARE @RC INT
begin
   insert into mytable( no ) values( @no )
   if ( @@ERROR <> 0 )
     SELECT @RC=-1
       return @RC
end
go

解决方案 »

  1.   

    to leimin:
    仍是没有执行下面的错误处理语句
      

  2.   

    那就insert后select一下,判断结果对不对
      

  3.   

    create procedure myProc( @no char(20) )
    as
    declare @rtt char(1)
    begin
    if not exists (select * from sysusers where name = 'mytable')
    set @rtt ='A'
    else
    begin 
     insert into mytable( no ) values( @no )
     set @rtt ='B'
    end
    return @rtt
    go 
      

  4.   

    在错误处理之前加入
    IF @@ROWCOUNT = 0
    return -1
      

  5.   

    create procedure myProc( @no char(20) ='')
    as
    begin TRAN
       if NOT exists (select * from sysobjects where id = object_id(N'[mytable]') and OBJECTPROPERTY(id, N'IsTABLE') = 1)
       GOTO ERRORFLAG
       insert into mytable( no ) values( @no )
       if ( @@ERROR <> 0 ) GOTO ERRORFLAG
       
       COMMIT TRAN
       RETURN 0ERRORFLAG:  
         ROLLBACK TRAN
         return -1
      
    go
      

  6.   

    各位老大,现在的情况是,如果sql语句执行出现了错误,如表不存在,列名不对等,则存储过程就直接返回,根本不执行随后的任何语句,因此我写的存储过程中的错误处理根本不起作用。
      

  7.   

    你这样写就可以了
    create procedure myProc( @no char(20) ='')
    as
    begin TRAN
       if NOT exists (select * from sysobjects where id = object_id(N'[mytable]') and OBJECTPROPERTY(id, N'IsTABLE') = 1)
       GOTO ERRORFLAG
       insert into mytable( no ) values( @no )
       if ( @@ERROR <> 0 ) GOTO ERRORFLAG
       
       COMMIT TRAN
       RETURN 0ERRORFLAG:  
         ROLLBACK TRAN
         return -1
      
    go
      

  8.   

    有一个参数可以设置存储过程出现错误报错还是继续执行,但是什么我忘了,不好意思!我的存储过程只要出错就推出,可是我同事的就继续执行,我现在还没找到原因:(,不知道是那个参数了,cry