How does SQL Server treat statements inside stored procedures with respect to transactions?

后端 未结 3 1866
有刺的猬
有刺的猬 2021-02-06 20:52

Say I have a stored procedure consisting of several separate SELECT, INSERT, UPDATE and DELETE statements. There is no explicit BEGIN TRANS / COMMIT TRANS / ROLLBACK TRANS logic

相关标签:
3条回答
  • 2021-02-06 21:19

    There will only be one connection, it is what is used to run the procedure, no matter how many SQL commands within the stored procedure.

    since you have no explicit BEGIN TRANSACTION in the stored procedure, each statement will run on its own with no ability to rollback any changes if there is any error.

    However, if you before you call the stored procedure you issue a BEGIN TRANSACTION, then all statements are grouped within a transaction and can either be COMMITted or ROLLBACKed following stored procedure execution.

    From within the stored procedure, you can determine if you are running within a transaction by checking the value of the system variable @@TRANCOUNT (Transact-SQL). A zero means there is no transaction, anything else shows how many nested level of transactions you are in. Depending on your sql server version you could use XACT_STATE (Transact-SQL) too.

    If you do the following:

    BEGIN TRANSACTION
    
    EXEC my_stored_procedure_with_5_statements_inside @Parma1
    
    COMMIT
    

    everything within the procedure is covered by the transaction, all 6 statements (the EXEC is a statement covered by the transaction, 1+5=6). If you do this:

    BEGIN TRANSACTION
    
    EXEC my_stored_procedure_with_5_statements_inside @Parma1
    EXEC my_stored_procedure_with_5_statements_inside @Parma1
    
    COMMIT
    

    everything within the two procedure calls are covered by the transaction, all 12 statements (the 2 EXECs are both statement covered by the transaction, 1+5+1+5=12).

    0 讨论(0)
  • 2021-02-06 21:24

    You must understand that a transaction is a state of the session. The session can be in an explicit transaction state because there is at least one BEGIN TRANSACTION that have been executed in the session wherever the command "BEGIN TRANSACTION" has been throwed (before entering in a routine or inside the routine code). Otherwise, the state of the session is in an implicit transaction state. You can have multiple BEGIN TRANSACTION, but only the first one change the behavior of the session... The others only increase the @@TRANCOUNT global sesion variable.

    Implicit transaction state means that all SQL orders (DDL, DML and DCL comands) wil have an invisble integrated transaction scope.

    0 讨论(0)
  • 2021-02-06 21:31

    You can find out on your own by creating a small stored procedure that does something simple, say insert a record into a test table. Then Begin Tran; run sp_test; rollback; Is the new record there? If so, then the SP ignores the outside transaction. If not, then the SP is just another statement executed inside the transaction (which I am pretty sure is the case).

    0 讨论(0)
提交回复
热议问题