Clarity in Code

Commenting code is always a good idea and there are more ways than one to comment your code. When running a long series of SQL queries it can be useful to know what has run. If you have conditional statements you’ll want to know which result ran. If you are gathering metrics and have more than a few statements, it helps if you can match the results with the query.

By insering the command Print followed by an identifying phrase between single quotes, you can get this kind of identification on the Messages tab.

Let’s say you don’t use this and have SET STATISTICS IO and SET STATISTICS TIME set. Let’s also say you have 15 queries running in sequence. When you look at the Messages tab, you’re going to have a hard time figuring out what results go with which query, especially since some commands can return multiple sets of statistics. If you do use it and have say PRINT ‘Employee Stats’ before the first query, PRINT ‘Sales figures’ before the second, PRINT ‘Sales by quarter’ before the third and so on, then before the first set of statistics on the Messages tab you’ll see

Employee Stats

and before the second set of statistics

Sales figures

and so on. This will not only let you know which stats go with which query, it’ll also let you know if your query is running more processes than you expected.

This entry was posted in 2008, SQL Server and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>