I work for Redgate and write about products. I’ve got a series of SQL Prompt posts here on little things I like. SQL Prompt might be my favorite tool. SQL Prompt will be yours as well if you give it a try.
I can’t believe I haven’t built this snippet before now, but it’s not in my list. I’ve dealt with this for some time, but I decided enough was enough.
Here’s what I want. Note that all the code is inside a single comment.
Getting There
I often have some random notes that I want to keep in a comment. For example, I might get a list of tables like this:
If I highlight this code and hit Ctrl, I get a list of snippets that contain a certain token. In my case, I get:
If I select Comment, I get the code commented, but as single line quotes.
That’s OK, but if I am saving something like STATISTICS output, that’s not pretty or easy for me to read. I prefer a single comment, not a series of separate inline comments. This is even more annoying with code.
Let’s fix this with a snippet. Here’s my snippet code:
The $SELECTEDTEXT$ token will take whatever text you’ve highlighted and insert it into the token. In my case, I just want this commented out.
Let’s see how this works. Suppose I have this query:
I really want to get the results from STATISTICS IO and TIME as I’m tuning a few things here and I want to check how well my changes work.
Once I run this, I’d like to place the results with the query and see them to compare with the next iteration. I paste the results into the query window like this:
Now I can highlight those results and hit CTRL, type surr, and get this:
I hit tab and I have my notes commented out:
The next time I run the query, I can easily compare how things have changed:
I also use this when working through a list of results. I’ll get those in the query window, highlight them, and then surround them with a comment.
There are lots of places you might like to use this token with SQL Prompt. For more ideas, Phil Factor has a nice scenario for using this with other tokens in the Redgate Hub.
Give SQL Prompt a try today and see how it can improve coding and feel free to share your tips here.