Protecting your database when testing queries

Protecting your database when testing queries

I found myself having to create a migration to create new forms based on a parent, 8  new ones for 5 countries ( working on a dealership site). While doing this I realized that I was introducing bad data into my database as queries didn’t finish. 

I thought nothing of it until I realized I now had 20 copies of a particular object/row that caused my SQL script to fail even though it was wrong.

Luckily, a coworker introduced me to using TRY CATCH  in SQL.

  • BEGIN TRY  
  •        BEGIN TRAN  
  •        DO STUFF
  •        COMMIT TRAN  
  • END TRY  
  • BEGIN CATCH  
  •        ROLLBACK TRAN  
  • END CATCH 

This ensures that if you have any errors, that you can revert the query without actually causing issues in your database. 

No Comments

Post a Comment