I have seen transaction usage in some cases but never really understood in which situations they should be used. How and when transactions should be used (begin transaction statement)? I have read that Microsoft do not suggest to use transaction statements (commit, rollback) inside a trigger and stored procedure.
Share
Transactions can be used in conjunction with error handling in stored procedures or SQL scripts when inserting or manipulating data to make sure everything is consistent.
For example, if you have a stored procedure that inserts records into a parent table and a child table, you would want to make sure that the parent record gets inserted first; if it fails, you can rollback your changes so you don’t have an orphaned child record.
Erland Sommarskog has a great article on how to use error handling in SQL Server.
Finally, where has Microsoft suggested to not use transactions in stored procedures? I would think that stored procedures would be an ideal place to use them.