过程1:CREATE procedure test
@t     int
as
set nocount on
begin
update testtable set id=1 where id=2
end
GO
================
过程2:CREATE procedure test
@t     int
as
set nocount on
begin
begin tran
update testtable set id=1 where id=2
commit tran
end
GO过程2比过程1多了begin tran和commit tran。过程2是不是多余的写法?过程1会自动提交事务吗?谢谢!

解决方案 »

  1.   

    也就是说begin和end之间的所有操作语句默认就是一个事务对吧?
      

  2.   

    update testtable set id=1 where id=2 
    单条更新语句具有隐形事务。
      

  3.   


    see:if object_id('tb') is not null drop table tb
    if object_id('ta') is not null drop table ta
    go
    create table tb(id int, x varchar(10))
    create table ta(id int, x varchar(9))
    go
    insert tb select 1,10
    union all select 2,'0123456789'
    go
    update tb set x=x+'1'
    go新开窗口执行
    select * from tb
    /*
    10
    0123456789
    */
    insert ta select id,x from tb新开窗口执行
    select * from ta
    /*
    无记录
    */
      

  4.   

    过程1中是隐式事务
    过程2中是显式事务
    2中的begin tran 和commit tran可以不要