I open a new query window in SSMS and execute: EXEC stored procedure. The stored procedure contains BEGIN TRAN and COMMIT / ROLLBACK TRAN.
I hit the cancel button and get told it has been done, almost instantaneously. When I then close the query window, I get the message: "There are uncommitted transactions. Do you wish to commit these transactions before closing the window?"
I thought that cancelling the query automatically initiated a rollback? If so, why am I being asked to decided whether to commit or rollback the transaction?
No. For an explicit transaction, you need an explicit COMMIT or ROLLBACK.
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
August 26, 2022 at 8:42 pm
Scott P. - Just so I get a better understanding, had the OP used a TRY/CATCH set of blocks, would the cancellation of the query in SSMS have triggered the CATCH block ?
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
August 26, 2022 at 9:43 pm
It depends. Unfortunately SQL can only trap certain errors; for some errors, even a CATCH won't "catch" an error.
For example, if you use and invalid column name -- one that does not appear in the table -- SQL won't transfer control to the CATCH block but will abort instead.
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
August 26, 2022 at 9:46 pm
I guess the only way to find out is to test it, then...
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
August 26, 2022 at 10:56 pm
I open a new query window in SSMS and execute: EXEC stored procedure. The stored procedure contains BEGIN TRAN and COMMIT / ROLLBACK TRAN.
I hit the cancel button and get told it has been done, almost instantaneously. When I then close the query window, I get the message: "There are uncommitted transactions. Do you wish to commit these transactions before closing the window?"
I thought that cancelling the query automatically initiated a rollback? If so, why am I being asked to decided whether to commit or rollback the transaction?
Cancelling a query doesn't necessarily mean cancelling a transaction. With that being said, look into SET XACT_ABORT ON. It fixes a wealth of such issues.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 27, 2022 at 4:36 pm
Scott P. - Just so I get a better understanding, had the OP used a TRY/CATCH set of blocks, would the cancellation of the query in SSMS have triggered the CATCH block ?
The stored procedure did have a TRY / CARCH block in it.
As SSMS knows an action has to be taken by the user - to commit or rollback - why not ask this on hitting the cancel button, rather than leaving things hanging.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply