2016-04-27 57 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? 

回答