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
原文:http://www.cnblogs.com/PeaCode/p/3836961.html