Automate Standard Comment Blocks – If you write a lot of SQL stored procedures or functions in SQL Server Management Studio, you might like this tip. Many companies require a standard comment format at the beginning of a stored procedure or function. What would be the easiest way to always have this text available?
Looking for something else? Try these links instead:
- Shrink Bloated Development Databases – Save Precious Space
- Azure Databases – Restore Local Database To Azure
- Execute Script Error Resolved In SQL Server Management Studio
- ApexSQL Provides Excellent SQL Tools For Free
- Easily Generate Table Change Scripts In SQL Server Management Studio
Some might think to have it in a text file and just copy and paste every time you create a new script. Here is a little trick for those lazy developers (in other words, everyone).
Automate Standard Comment Blocks
Lets assume that I need to add the following text to every script I write. This is company policy and I have to do it. It might contain some sort of copyright notice or support information or whatever. The bottom line is that after a few months of manually copy and pasting this text, you might be a bit fed up.
If that fed up person is you, then head on over to the following path (on 64-bit PC’s):
[code language=”text”]C:Program Files (x86)Microsoft SQL Server100ToolsBinnVSShellCommon7IDESqlWorkbenchProjectItemsSql[/code]
Look for a file called SQLFile.sql. Edit this file using your favourite text editor (mine being SciTE) and add the contents of your standard comment to this file and save.
Restart SQL Server Management Studio and open a new Query window by clicking on the ‘New Query’ button.
Your standard comment text is pre-populated in your query window. This is especially nice when you have lots of text you need to add each time. It is definitely a time saver.
Oddly enough, this doesn’t work if you right click on a database in the Object Explorer and select ‘New Query’ from the context menu. If anyone has an answer as to why, I’d love to know. Lastly, this file is obviously only on your local machine. Each user that needs to have this default comment inserted into new queries will need a copy of the SQLFile.sql file.