Selecting and Executing Commented T-SQL Code
This may seem like a trivial “trick”. I thought so too at first, but I’ve found that I use this trick over and over again.
When developing scripts, often times I find myself commenting some blocks of code out, either to explore, to test, to debug, or to simply try out other T-SQL constructs. When you have commented code using the C-style comment, it is easy to select any block of code inside and execute them:
SELECT * FROM sometable;
SELECT * FROM anothertable;
But what if you used SQL Server Management Studio’s (SSMS) single line comment shortcut to comment out parts of your code?
Of course you can use the uncomment shortcut, but that’s still a bit of work. Highlight code, move mouse to SSMS toolbar, click on icon, execute. No I’m not a whiner, I just like doing things more efficiently when possible. Imagine, take that one icon click, and if you do it 10x or 20x in a day, it adds up 🙂
What you can do (or try) is to press the ALT key, then highlight the code you want to execute, then execute.
Small trick that saves you one icon click. Seemingly trivial, but believe me, it does make your work faster.