This month’s T-SQL Tuesday invitation comes from Camila Henrique who asks us to go back in time and give some T-SQL advice to our younger selves. It’s not hard for me to think of a few tidbits of advice. Check out the topic invitation and more by clicking on the image below:
Typing on Multiple Lines at Once
A quick T-SQL/SSMS trick that I know and love is the ability to add text to multiple lines at a time. I tend use macros in Notepad++ to accomplish these types of repetitive actions but this type of work can be completed in SSMS.
Let’s say we are searching a table for specific names and a list of names is given to us to paste into our query:
We can go to each line and add our single quotes and commas which can be time consuming depending on the size of the list. A quicker method is to go to the beginning of our names (in this case, the beginning of Chad), hold Shift+Alt, and press the down arrow until we reach the bottom of our list:
We’ll see a vertical line in front of each name. We can type our single quote and it will show on all three lines:
We can go to the end of the names and do the same for our closing single quote and comma:
We can remove that last comma and run our query.
This was a cool trick I liked the first time I saw it and hated that I hadn’t known about it sooner.
What Were You Thinking
I would tell my younger self to always take the extra few minutes to comment code. I’ve gotten better over the years but there are still times I come across something old and wonder what I was thinking. Never assume something is simple enough and tell yourself “I’ll surely remember what this means.”
To be fair, there are also times I come across something I wrote last week and wonder what I was thinking. Commenting your code makes life easier on your future self.
Thanks for reading!