Safeguard to Avoid Costly Mistakes for UPDATE or DELETE Statements in SQL Server Management Studio
Iíve talked to many people who have opened up a new query window in SQL Server Management Studio, fired off an UPDATE or DELETE statement, and then once the query finished, they realized that they forgot the WHERE clause (or forgot to highlight it). The story usually then takes a turn for the worse: they had to restore from backup or, perhaps due to lack of backups, they had to dust off their resume. Iím sure many people have frantically pressed the Undo button on the SSMS toolbar, hoping it would affect their query instead of the text editor. Wouldnít it be nice if there was an Undo Last Query button?
SQL Server doesnít support Ctrl + Z, but you can protect yourself in other ways. A previous tip showed how you can change the color of your status bar, making it more obvious that you are connected to critical environments; I can tell you from personal experience that, sometimes, that isnít enough.
Another way you can protect yourself Ė and which I started doing about a decade ago Ė is to make sure that all new query windows start with an explicit transaction. You can do this by modifying the new query template, which pulls from a file named SQLFile.sql. The location of this file will vary by version of SSMS; on my machine, since Iím running the latest (17.0), the file is in:
C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\
For older supported versions, you will likely have to change 140 to 130, 120, or 110. On really ancient versions of Management Studio, you should just upgrade. But back then the location was something like:
C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\
And similarly, you may have to change 100 to 90 if you are using SQL Server 2005.
If you navigate to this folder and open SQLFile.sql, youíll see that itís empty. Before you do anything, you should right-click the file and ensure that it is not read only, and that users can modify the file (which may require additional prompts from UAC, if that is enabled):
Once you can modify the file, you can simply add the following lines of code, and then save:
-- COMMIT TRANSACTION;
-- ROLLBACK TRANSACTION;
Now when you click the New Query toolbar button, or Ctrl + N, this is what you should see:
Note: Modifying this file does not affect the right-click context menus in Object Explorer, even though those are also called New Query. If you feel this would be worth making consistent (I do!), please vote for this Connect item Ė even though itís closed. Comments are still accepted too, and they carry more weight than votes anyway.
So you might ask, how does this help you? Well, if you open a new query window quickly, and type up a query and hit F5 or Ctrl + E, you can validate that the right data was affected before highlighting the COMMIT TRANSACTION portion and executing that. If youíve made a mistake, you can highlight the ROLLBACK TRANSACTION portion instead, so that the only damage youíve caused was for the duration of the transaction being active.
Of course, this can cause a different problem: You run your query in this transaction, forget to commit, and then go to lunch or go home for the day. This means that you will hold all of the locks youíve taken, potentially until someone manually kills your session. So be careful out there!
- Modify SQLFile.sql to include explicit transactions. You can also modify the file to include default comments or commonly used commands.
- Review these tips and other resources:
- Assign Colors in SQL Server Management Studio Query Windows Based on Environment
- Customizing the Status Bar for each SQL Server SSMS Connection
- SQL Server 2016 Management Studio Enhancements
- New Features in SQL Server Management Studio for SQL Server 2016
- Download Management Studio 2016 or SSMS 17.0
- All Management Studio tips
Last Updated: 2017-05-15
About the author
View all my tips