I would like to run some sql command as a transaction:
If one sql statement because of any reason doesn't work, the other statements should not affect the database. To achieve this aim I have written this block of code:
DECLARE @TransactionName VARCHAR(20)= 'Demotran1';
BEGIN TRANSACTION @TransactionName
-- This statement works
alter table [SalesLT].[ProductModel]
add test315 int
-- This statement doesn't work
alter table [SalesLT].[ProductModel]
add testtimestamp123 timestamp
rollback TRANSACTION @TransactionName
After running the above statement I have this expectation that nothing is happening to Table ProductModel This code works for me BUT after running it, for some minutes my whole database is somehow not accessible. My first question is, why the database after running this command behaves like this? and Do you have a better idea to achieve my aim?