Can someone help me refactor this code so in case of an error nothing gets persisted to the db? this doesn't work when an error occurs.
use mydb
Begin Transaction
SET IDENTITY_INSERT table1 On
Insert into table1 (...) values (...)
SET IDENTITY_INSERT table1 Off
If @@error <> 0 GOTO done
SET IDENTITY_INSERT table2 On
Insert into table2 (..) values (...)
SET IDENTITY_INSERT table2 Off
If @@error <> 0 GOTO done
Commit Transaction
Return
done:
Rollback Transaction
Return