{"id":414,"date":"2025-02-07T18:30:00","date_gmt":"2025-02-07T18:30:00","guid":{"rendered":"https:\/\/imcodinggenius.com\/?p=414"},"modified":"2025-02-07T18:30:00","modified_gmt":"2025-02-07T18:30:00","slug":"sql-server-transaction-basics","status":"publish","type":"post","link":"https:\/\/imcodinggenius.com\/?p=414","title":{"rendered":"SQL Server Transaction basics"},"content":{"rendered":"<h3>1. Microsoft SQL Server Transaction<\/h3>\n<p>  SQL Server Transaction creates a wrapper around the set of SQL Statements.<br \/>\n  Converts multiple SQL statements into a single unit of work.<br \/>\n  The single unit of work is entirely completed or completely undone.<\/p>\n<h3>2. Data Inconsistency<\/h3>\n<p>  The stored procedure execution doesn\u2019t always complete as expected. There could be unforeseen scenarios like network error interruption, memory module corruption, natural disasters in data center locations and any number of things that may happen during the process that cause one activity to finish successfully and the other activity to fail.<\/p>\n<p>      Account 1<br \/>\n      Account 2<\/p>\n<p>      500<br \/>\n      0<\/p>\n<p>      -100<br \/>\n      0<\/p>\n<p>      400<br \/>\n      +100<\/p>\n<p>      Bal: 400<br \/>\n      Bal: 100<\/p>\n<p>In this bad situation, the data will be in an inconsistency state.<br \/>\nEx: Account 1 will be debited with 100, but Account 2 doesn\u2019t receive it; hence, the total won\u2019t tally.<\/p>\n<p>The solution to the problem is to wrap the debit and credit process into a single unit of work so that both operation will be completed successfully.<\/p>\n<p>The transaction completion is called COMMIT, and the new values will be permanently written to disk.<\/p>\n<p>If any SQL statement fails, then both the statements are undone. The debit and credit changes will be reverted back to the previous state.<\/p>\n<p>The process of undoing all the actions performed within the transaction is called rollback.<\/p>\n<p>The wrapping of SQL statements within the transaction helps to ensure the data remains consistent and the trustworthy.<\/p>\n<h3>3. Create a Transaction<\/h3>\n<p>BEGIN TRANSACTION or BEGIN TRAN to begin the transaction.<\/p>\n<p>You can provide a name for the transaction, and that\u2019s optional.<\/p>\n<p>BEGIN TRANSACTION  Ex: BEGIN TRANSACTION Transaction1.<\/p>\n<p>WITH MARK (optional) &#8212; When you add a mark to the transaction, then the record of the transaction\u2019s name is added to the database transaction log, which is helpful when you want to restore the database to the prior state.<\/p>\n<p>Mark helps to identify the exact date and time the transaction is completed successfully.<\/p>\n<p>\n    Fig 1: Create a transaction<\/p>\n<h3>4. Group of SQL statements inside Transaction<\/h3>\n<p>  Group of statements inside the transaction.<\/p>\n<p>\n  Fig 2: Group of  SQL statements<\/p>\n<p>  Before inserting the values into the IncomeSourceType table.<\/p>\n<p>\n  Fig 3: Table values<\/p>\n<p>  After INSERT statement execution<\/p>\n<p>\n  Fig 4: After Insert statement execution<\/p>\n<p>  After the INSERT statement, execute the IncomeSourceType table.<\/p>\n<p>\n  Fig 5: After Insert statement execution &#8212; select table<\/p>\n<p>It looks like 2 rows were newly added to the table. It is still inside the transaction, and the data is not yet finalized on the server.<\/p>\n<p>  Execute the select statement in the new window, and the results are<\/p>\n<p>\n  Fig 6: Select table in new session<\/p>\n<h3>5. Rollback<\/h3>\n<p>  Rollback the transaction.<\/p>\n<p>\n    Fig 7: Rollback transaction<\/p>\n<p>  Newly inserted 2 records are roll back.<\/p>\n<p>\n  Fig 8: Select table after Rollback<\/p>\n<h3>6. Commit<\/h3>\n<p>  What if you \u2018COMMIT\u2019 the transaction instead of rolling it back? Then the results are<\/p>\n<p>\n  Fig 9: Commit transaction<\/p>\n<p>Results after \u2018COMMIT\u2019 transaction<\/p>\n<p>\n  Fig 10: Results after Commit transaction<\/p>","protected":false},"excerpt":{"rendered":"<p>1. Microsoft SQL Server Transaction SQL Server Transaction creates a wrapper around the set of SQL Statements. Converts multiple SQL statements into a single unit of work. The single unit of work is entirely completed or completely undone. 2. Data Inconsistency The stored procedure execution doesn\u2019t always complete as expected. &#8230; <\/p>\n<div><a class=\"more-link bs-book_btn\" href=\"https:\/\/imcodinggenius.com\/?p=414\">Read More<\/a><\/div>\n","protected":false},"author":0,"featured_media":415,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-414","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts\/414"}],"collection":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=414"}],"version-history":[{"count":0,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts\/414\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/media\/415"}],"wp:attachment":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}