表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存储过程就会有重复现象,应该如何避免此类问题? 

解决方案 »

  1.   

    create proc aaa 
    as 
    begin tran
    update  表1 
    set 单价=单价*1.2 
    where 类别=0 
    update 表1 
    set 类别=1 
    where lb=0 
    commit tran加个事务
      

  2.   

    begin try 
    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
      

  3.   

    具体怎么加事务》?
    好像要用到 BEGIN TRANSACTION
    该怎么加?能否详细说明一下、?谢谢
      

  4.   

    我上面不是写了么?1楼。。
    begin tran commit tran 
      

  5.   

    begin 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'  是什么意思?
      

  6.   


    服务器: 消息 170,级别 15,状态 1,行 1
    第 1 行: 'try' 附近有语法错误。
    服务器: 消息 156,级别 15,状态 1,行 11
    在关键字 'end' 附近有语法错误。
      

  7.   

    正解!
    这是个try..catch结构!
    参见http://msdn.microsoft.com/zh-cn/library/ms175976(SQL.90).aspx
      

  8.   


    --高建刚推荐事务写法
    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
      

  9.   

    Try Catch毕竟SQL Server2000不支持啊。
      

  10.   

    SQLserver是用事务来保持完整性的事务的开始用
    begin tran 结束用
    commit tran 如果出错用
    ROLLBACK   TRAN