![]() |
|
|
By: Jugal Shah | Read Comments (8) | Print Jugal has 8+ years of extensive SQL Server experience and has worked on SQL Server 2000, 2005, 2008 and 2008 R2. Related Tips: More |
|
As a best practice, most companies follow a standard comment format at the beginning of stored procedures and functions. In addition, it is also a good idea to include comments in all scripts you write even if they are just save as .sql or .txt files. Manually we often add a standard comment block to the header of our scripts using copy and paste, but in this tip we look at how to automate adding a standard comment block for every set of code we write using SSMS.
As a best practice you should create a standard comment format at the beginning of your T-SQL codes such as the below example. If you are SQL Developer and your job is to only write stored procedures and functions, you can automate adding a standard comment header when you open a new Query window.
--Sample Comment format /* ************************************************************************************* [OBJECT NAME]: Stored Procedure Name [DESCRIPTION]: SP Description Copyright by Company Name [NOTES]: [PARAMETER BREAKDOWN]: @parameter name - Parameter description @parameter name - Parameter description [SAMPLE CALL]: exec spName @parameter1 = 'MSSQLTips', @parameter2 = 'Jugal', [SQL Version Support]: SQL Server 2000, 2005, 2008 [MODIFICATION HISTORY]: Date Author Comment ----------- ---------------------- ----------------------------------------- 6/8/2009 Jugal Shah Inception 6/23/2009 Jugal Shah add dbmail feature ************************************************************************************* */
If you don’t want a standard comment block another option it so include frequently used T-SQL statements such as the below commands.
--Sample T-SQL Code select @@servername select @@version select * from sys.sysprocesses where blocked <> 0 sp_who2 active sp_heldb xp_fixeddrives
So if I use the second example, when I open a new query window in SSMS I get these commands every time as shown below.

To automate the comment/T-SQL in all new query windows, you have to modify the SQLFile.sql file which is located in:
You could also do a search for SQLFile.sql as well. The below image shows the file.

Once you find the file, you can edit the SQLFile.sql with any text editor (i.e. notepad) and add your own standard comment format or T-SQL code and save the file. Then whenever you open a new Query window you will see the comments/T-SQL that you entered into the SQLFile.sql file.
Note that you must have the proper permission to edit SQLFile.sql. I used Notepad and used the "Run as administrator" option to edit and save the file. This just depends on the security that was setup on your desktop. Also, since this file is saved locally each person that wants this has to do this on thier machine.
| Share: | Share | Tweet |
|
![]() |
![]() |
Free SQL Server Learning |
| Wednesday, May 04, 2011 - 9:41:57 AM - Bob | Read The Tip |
|
I really appreciate these tips in my RSS feed each day! Based on the code in your comment area, I have one tiny addition that I've found tremendously helpful over the years (learned by watching someone else code, of course). Often people leave commas at the end of a line, like in your comment section example:
exec spName @parameter1 = 'MSSQLTips', @parameter2 = 'Jugal' However, if you place commas at the beginning of the NEXT line, you can comment out and/or highlight just the lines you want to use and even provide alternatives embedded in the code that you can active/de-activate, like in these examples:
exec spName @parameter1 = 'MSSQLTips' , @parameter2 = 'Jugal' , @parameter3 = 'do not highlight this line when running the code and you get the default instead' SELECT TOP 100 * FROM Orders o -- INNER JOIN OrderItems oi ON o.orderID = oi.orderID INNER JOIN Customers c ON o.customerID = c.customerID SELECT o.orderID --, o.orderDate , o.orderTotal , c.companyName , c.phone FROM Orders o INNER JOIN Customers c ON o.customerID = c.customerID -- WHERE o.orderTotal > 1000
|
|
| Wednesday, May 04, 2011 - 10:03:05 AM - Greg Robidoux | Read The Tip |
|
Hi Bob, thanks for the great tip. If you are interested in writing a tip about this, please let us know. |
|
| Wednesday, May 04, 2011 - 11:55:46 AM - dj | Read The Tip |
|
Doesn't work for me on XP (x86) or Win7 (x64). :( |
|
| Wednesday, May 04, 2011 - 12:07:18 PM - Jugal Shah | Read The Tip |
|
Seach for the SQLFile.SQL using Operating System search functionality. Let me know if you will get the file. |
|
| Wednesday, May 04, 2011 - 12:30:40 PM - dj | Read The Tip |
|
Finding the file was no trouble. Editing and saving were no trouble either. I closed and opened SSMS (2008 R2) but still get blank new queries.
|
|
| Wednesday, May 04, 2011 - 12:49:35 PM - Jugal Shah | Read The Tip |
|
Have you saved the SQLFile.SQL after editing? |
|
| Wednesday, May 04, 2011 - 1:00:44 PM - dj | Read The Tip |
|
Of course. |
|
| Wednesday, May 11, 2011 - 9:29:32 AM - Samson J. Loo | Read The Tip |
|
This works out great. I am running Windows 7 x64 Home Premium with SQL Server 2008 R2 Developer x64 and I found my file located at: C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\SqlWorkbenchProjectItems\Sql. |
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |