2016-04-27 30 views
1

请参阅下面的DDL:Commiting外部事务

create table dbo.Test (id INT) 

create PROCEDURE TestProcedure1 
as 
begin 
    exec TestProcedure2 
    insert into Test values (2) 
end 


create PROCEDURE TestProcedure2 
as 
begin 

begin transaction 
    insert into Test values (2) 
end 

现在运行此:

exec TestProcedure1 commit 

错误输出低于:

(1 row(s) affected) 
Msg 266, Level 16, State 2, Procedure TestProcedure2, Line 0 
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1. 

(1 row(s) affected) 
Msg 266, Level 16, State 2, Procedure TestProcedure1, Line 0 
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1. 

我理解错误的原因即TestProcedure2中没有结束事务。这有什么影响?我看到的错误后,我跑到下面的SQL语句:

select * from dbo.Test 

这回两行即外部事务提交的内部事务。我有两个问题:

1) Does the outer transaction always commit the inner transaction. 
2) After executing the select statement I executed the following commands: 

commit 
commit 

What affect does running two commit statements have in this scenario? 

回答