表1
编号 数量 单价 金额 类别
01 2 2 4 0
02 2 3 6 0
03 1 5 5 0
04 3 2 6 0
...............
create proc aaa
as
update 表1
set 单价=单价*1.2
where 类别=0
update 表1
set 类别=1
where lb=0
在执行aaa存储过程中如果只运行了前半部分语句电脑忽然断电,下半部分语句未执行, 但是前面的已经修改过,下次在运行aaa存储过程就会有重复现象,应该如何避免此类问题?
编号 数量 单价 金额 类别
01 2 2 4 0
02 2 3 6 0
03 1 5 5 0
04 3 2 6 0
...............
create proc aaa
as
update 表1
set 单价=单价*1.2
where 类别=0
update 表1
set 类别=1
where lb=0
在执行aaa存储过程中如果只运行了前半部分语句电脑忽然断电,下半部分语句未执行, 但是前面的已经修改过,下次在运行aaa存储过程就会有重复现象,应该如何避免此类问题?
as
begin tran
update 表1
set 单价=单价*1.2
where 类别=0
update 表1
set 类别=1
where lb=0
commit tran加个事务
begin tran
update 表1
set 单价=单价*1.2
where 类别=0
update 表1
set 类别=1
where lb=0 commit tran
end try
begin catch
rollback
end catch
好像要用到 BEGIN TRANSACTION
该怎么加?能否详细说明一下、?谢谢
begin tran commit tran
create proc aaa
as
update 表1
set 单价=单价*1.2
where 类别=0
update 表1
set 类别=1
where lb=0
commit tran
就可以了吗?
'commit tran
end try
begin catch
rollback
end catch' 是什么意思?
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: 'try' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 11
在关键字 'end' 附近有语法错误。
这是个try..catch结构!
参见http://msdn.microsoft.com/zh-cn/library/ms175976(SQL.90).aspx
--高建刚推荐事务写法
create proc aaa
as
begin tran
update 表1
set 单价=单价*1.2
where 类别=0
update 表1
set 类别=1
where lb=0
if @@ERROR=0
begin
commit tran
end
else
begin
rollback Tran
end
begin tran 结束用
commit tran 如果出错用
ROLLBACK TRAN