1
我有兩個步驟:TSQL交易 - 提交和回滾
create procedure P2
as
begin
print @@trancount
begin tran
if 1 = 1
begin
print @@trancount
rollback
end
else
begin
commit
end
end
go
create procedure P1
as
begin
begin tran
print @@trancount
exec P2
print @@trancount
commit
end
go
exec P1
當我打電話P1我:
1
1
2
Msg 266, Level 16, State 2, Procedure P2, Line 0
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0.
0
Msg 3902, Level 16, State 1, Procedure P1, Line 8
The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.
我預期的結果是這樣的:
1
1
2
1
我的問題是:
1. Why do I got this error?
2. How should I write my procedure to do it good?