About caller there is a transaction, the SP is also called a transaction rollback error handling code in nested SP, the Good text recommended

SQL报错异常:Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0.

 

- First of all clear that the open transaction in SQL, when Begin Tran, @@ TRANCOUNT will add 1, @@ TRANCOUNT will be reduced by 1 Commit Tran, but when ROLLBACK TRAN when @@ TranCount will directly set to 0,
- - SQL code for externally open transaction, if SP nested calls is also open transaction, then the SP proceeds to when nested, @@ tranCount = 1 (if one nested long,
- where 1 is equal to the external call because SP where Begin Tran used to open a transaction), but if the use of nested code rollback rOLLBACK TRAN SP, the @@ tranCount will directly set to 0,
- in this will lead to the number of nested SP @@ tranCount come in and go out inconsistent, SQL will be given at the end of the call, so the nested SP in a rollback if needed, should be used to roll back the transaction to save points, rather than ROLLBACK TRAN.
- If the SP nested rollback if necessary, you should use PP TRAN ROLLBACK,
- if directly ROLLBACK TRAN, is performed ROLLBACK TRAN, @@ TRANCOUNT will directly set to 0,
- so that if the outer layer the SP has a place to call Begin Tran, it will lead to SQL reported similar
- ". Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements Previous count = 1, current count = 0." exception,
- Since entering the process, the external sql Begin tran will make @@ TranCount plus one, but ROLLBACK TRAN perform here, would @@ TRANCOUNT directly set to 0,
- when the SP returns the end of the call, the transaction the number of calls the SP and the end is not the same, so the SQL reported abnormal.
- The solution to this problem is to be kept in the SP if there is a transaction, then the nested SP, open transaction nested transaction point, Tran PP as the Save;
- in Catch the need to roll back code or place plus rOLLBACK TRAN pp; that is to roll back the current transaction code, the code will not change the value of @@ TranCount.
- COMMIT TRAN ;, then this will write the nested BEGIN TRAN SP corresponding to release, i.e., that the BEGIN TRAN @@ TranCount plus 1, COMMIT TRAN; @@ TranCount so Save 1;
- call this and the number of @@ TranCount the end of the call nesting SP on the same, it will not report the above anomaly. You read that right, to use the rollback transaction savepoint rollback in a nested SP, the
- and the need Commint transactions before they can run properly. That is, if there is external code called from the transaction, and the SP also has nested in a transaction, then the transaction regardless of the nested SP commit or roll back, we need to commit the transaction can, otherwise it will report the above error.
- Practice has proved that call nested SP place not only in SQL sp have this problem, C # code from a transaction, SP has come to call the nested transaction processing,
- Write a rollback statement directly in the nest in SP rOLLBACK TRAN will have the same problem, you can roll back the transaction using point to resolve. Remember when using nested SP in a transaction, regardless of whether a rollback is required before they can commit the transaction.
- Of course, if the caller did not play in the SP or SQL transactions, SP has been called in the transaction, use the ROLLBACK TRAN has no effect, since the transaction has nested transaction must pay special attention, indeed practice makes perfect.








---创建测试表 IF EXISTS ( SELECT * FROM sys.tables WHERE name = 'tt' ) DROP TABLE dbo.tt ; CREATE TABLE dbo.tt ( ID INT IDENTITY , Name NVARCHAR (100), TransCount INT ) ; GO --查询结果 SELECT * FROM dbo.tt ---创建子存储过程 IF EXISTS ( SELECT * FROM sys.procedures WHERE name = 'S_proc' ) DROP PROC S_proc ; GO CREATE PROC S_proc AS BEGIN PRINT('s-init-' + CAST(@@TRANCOUNT AS NVARCHAR(10 ))); the BEGIN TRAN; Save Tran PP; the PRINT ( ' S-prev- ' + the CAST (@@ TRANCOUNT the AS NVARCHAR ( 10 ))); the DECLARE the INT P2 @ = @@ TRANCOUNT; the INSERT the INTO dbo.tt (the Name , TransCount) the SELECT ' query 2 ' , @ P2; - If you want to roll back here, you should use the PP TRAN rOLLBACK, - if the direct use rOLLBACK TRAN, execute rOLLBACK TRAN, will directly set @@ TRANCOUNT 0, - so that if the outer call the SP where there Begin Tran, it will lead to SQL reported similar -. "EXECUTE indicates the After COUNT Transaction Number the a Mismatching of BEGIN and COMMIT statements Previous COUNT = 1 , Current COUNT = 0. "Exception - since entering the process, the external sql Begin tran will make @@ TranCount plus one, but ROLLBACK TRAN perform here, would @@ TRANCOUNT directly set to 0, - ending the call when the SP return, the number of calls the transaction and the SP is not the same at the end, so the SQL reported abnormal. - way to solve this problem is nested SP if there is a transaction, then the nested SP, open save local affairs point affairs, Tran PP such as the save; - places Catch the need to roll back or code plus rOLLBACK TRAN pp; that is to roll back the current transaction code, the code will not change the value of @@ TranCount. - - then this will write the COMMIT TRAN ;, nested BEGIN TRAN SP corresponding to release, i.e., that the BEGIN TRAN @@ TranCount plus 1, COMMIT TRAN; @@ TranCount so Save 1; - this call and the end @@ TranCount when the number of calls to the same nesting SP, it will not report the above exceptions you read right, is to use the save point to roll back the transaction if rollback nested SP, - and the need Commint Affairs before they can run properly. in other words, if the call on behalf of external Since there is a transaction, and the SP also has nested in a transaction, then the transaction regardless of the nested SP commit or roll back, you need to commit the transaction can, otherwise it will report the above error. - Practice has proved that call nested SP where not only the sp have this problem SQL, C # code from a transaction, SP again calls have nested transaction processing, - written directly nested SP in a rollback ROLLBACK TRAN statement will have the same problem, you can roll back the transaction using point to resolve. Remember when using nested SP in a transaction, regardless of whether a rollback is required before they can commit the transaction. - Of course, if the caller did not play in the SP or SQL transactions, SP has been called in the transaction, use the ROLLBACK TRAN has no effect, since the transaction has nested transaction must pay special attention, indeed practice makes perfect. - remember here not directly write ROLLBACK TRAN; but should write ROLLBACK TRAN pp savepoint rollback transaction following; ROLLBACK TRAN pp; the PRINT ( ' S-After-rollback- ' + the CAST (@@ TRANCOUNT the AS NVARCHAR ( 10 ))); - remember here whether or not to roll back, you need to commit the transaction, will not be reduced or @@ TRANCOUNT 1, 1 @@ TRANCOUNT add up into the sky when the transaction will lead to inconsistencies in the number of incoming and outgoing transactions mistake. TRAN a COMMIT; the PRINT ( ' S-after- ' + the CAST (@@ TRANCOUNT the AS NVARCHAR ( 10 ))); the RETURN; the END; ---创建主存储过程 IF EXISTS ( SELECT * FROM sys.procedures WHERE name = 'P_proc' ) DROP PROC P_proc ; GO CREATE PROC P_proc AS BEGIN DELETE FROM dbo.tt; PRINT('p-init-' + CAST(@@TRANCOUNT AS NVARCHAR(10))); BEGIN TRAN ; PRINT('p-prev-' + CAST(@@TRANCOUNT AS NVARCHAR(10))); DECLARE @p1 INT = @@TRANCOUNT; INSERT INTO dbo.tt ( Name, TransCount ) SELECT '查询1', @p1 ; PRINT('p-after-insert-' + CAST(@@TRANCOUNT AS NVARCHAR(10))) EXEC dbo.S_proc ; PRINT('p-after-' + CAST(@@TRANCOUNT AS NVARCHAR(10))); COMMIT TRAN; PRINT('p-last-' + CAST(@@TRANCOUNT AS NVARCHAR(10))); RETURN ; END ; GO

 

Guess you like

Origin www.cnblogs.com/itjeff/p/12106447.html