--开始事务
begin tran
--定义变量,存储错误变量
declare @cw int
set @cw=0
update B set sex=2 where name='张三'
--用@@ERROR判断语句执行是否成功
set @cw+=@@ERROR
update B set sex=1 where name='王五'
set @cw+=@@ERROR
--判断语句执行是否成功
if(@cw0)
begin
--不等于0,语句出错,回滚事务
print '语句执行失败,已回滚'
rollback tran
end
else
begin
--等于0,提交事务
print '语句执行成功,已提交'
commit tran
end
代码:
--判断触发器是否存在
if exists(select * from sys.triggers where name='trig_update')
drop trigger trig_update
go
--创建触发器
create trigger trig_update
--在A表中创建
on A
--修改触发器
for update
--当触发触发器后要做的操作
as
--定于变量存储错误信息
declare @a int
set @a=0
update B set chusheng+=1 where name='王五'
set @a+=@@ERROR
--不等于0则修改失败
if(@a0)
begin
print '王五信息修改失败'
end
else
begin
print '王五信息修改成功'
end
go