Question

Reading MS documentation on different transaction modes in SQL Server. Autocommit mode does everything the Implicit and Explicit Transaction mode does with less code, so why should I use Implicit and Explicit Transaction modes in my code ?

Was it helpful?

Solution

Autocommit transaction is only for single query. If you need transaction involving multiple queries, you must use Implicit and Explicit Transaction mode.

OTHER TIPS

As you know sqlserver automatically done the job of transaction commit. But some time we need to commit /rollback on particular condition/logic/business rule(s).

For example, we have one master table and 3 child/details table or say 1 or more child tables. Suppose, we must save master table entry along with all details table with reference of master table's pk-id. In any case anything an issue then revert whole thing.

So in this scenerio we need to use explicit transaction to commit or rollback as a unit of work. We can use try..catch block for error handling and rollback the transaction.

If we not used this transaction, then after each insert statement sqlserver auto-commit the inserted row and not rollback ever.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top