带"回滚"功能的存储过程

Create PROC [ChangeDept]
(
@FromUserID varchar(25),  
@ToDeptID int             
)
As
Begin
  BEGIN TRANSACTION     --开始事务
  DECLARE @errorSun INT --定义错误计数器
  SET @errorSun=0 --没错为0

  
  declare @RoleID varchar(25)
  select @RoleID=RoleID from Role where RoleName='sales'
  
  
  update Employee set DepartmentID=@ToDeptID,RoleID=@RoleID where EmployeeID=@FromUserID
  SET @errorSun=@errorSun+@@ERROR --累计是否有错
   
  update Project set DepartmentID=@ToDeptID  where  EmployeeID=@FromUserID
  SET @errorSun=@errorSun+@@ERROR --累计是否有错

  
  IF @errorSun<>0
    BEGIN
        PRINT '有错误,回滚'
        ROLLBACK TRANSACTION--事务回滚语句
    END
  ELSE
    BEGIN
        PRINT '成功,提交'
        COMMIT TRANSACTION--事务提交语句
    END
End

 

posted @ 2014-07-11 09:02  裸奔的豆子  阅读(181)  评论(0编辑  收藏  举报