You are here
Home > SQL > Automate Standard Comment Blocks For New SQL Queries

Automate Standard Comment Blocks For New SQL Queries

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? 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.

Automate Standard Comment Blocks

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.

Automate Standard Comment Blocks

Automate Standard Comment Blocks

Restart SQL Server Management Studio and open a new Query window by clicking on the ‘New Query’ button.

Automate Standard Comment Blocks

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.

Automate Standard Comment Blocks

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.

SQL Server forums

Dirk Strauss
Dirk is a Software Developer and Microsoft MVP from South Africa. He loves all things Technology and is slightly addicted to Twitter and Jimi Hendrix. Apart from writing code, he also enjoys writing human readable articles. "I love sharing knowledge and connecting with people from around the world. It's the diversity that makes life so beautiful." Dirk feels very strongly that pizza is simply not complete without Tabasco, that you can never have too much garlic, and that cooking the perfect steak is an art he has yet to master.

Similar Articles