T-SQL Shortcut for Editing Multiple Lines of Code

Reading Time: 4 minutes

Share:

Share on facebook
Share on twitter
Share on linkedin

This is a nifty little shortcut to help quickly edit multiple lines of code in a TSQL query window. I’ve personally found this TSQL shortcut extremely useful, and I hope you will too.

For this tip, I created a small scenario that includes a list of account numbers we might receive from the finance team to investigate further by running some queries.

tsql shortcut

tsql shortcut

I’m going to copy that list of account codes from the spreadsheet finance provided. With my simple select statement, I just want to create “In.” I will open parentheses at the end of the statement, add my single quote, and then paste those values.

tsql shortcut

tsql shortcut

And here is the tip that saves some time for finishing this list. GP always likes to give you these lists with many, many spaces in them, so the first thing I’m going to do is replace those empty spaces with a single quote.  Go to the end of the list, including all of those spaces. Now hold the shift key, and select all those empty spaces from that bottom line, the last account number in the list.

tsql shortcut

Now that I have that highlighted, here’s the tip.

Push and hold the shift key. While doing that, push and hold the alt key, so you have the shift and alt held down right now. Then push the up arrow to move up in the list. So I will leave the top one out of it for now.  If I don’t, it will highlight the last number and replace it, which we don’t want.  Try it for yourself so you can see.  When I let go of my shift and alt, I now have all of those lines highlighted in the same location.

tsql shortcut

Now hit the single quote key, which will replace all of those empty spaces with a single quote.

tsql shortcut

Now, manually add a single quote to the end of the top one that we ignored earlier.

We are not quite done yet.  We need to also add a comma to separate the list, and another single quote to identify the value. So again, put the cursor where you want to start. Press and hold the shift and alt keys, and then hit the down arrow. Now we can add a comma and a single quote, and finally close our parentheses here.

tsql shortcut

Thus, we have a very simple way of editing multiple lines in TSQL.

A Second TSQL Shortcut Example

Here’s a different example of how you can use the same TSQL shortcut.

Let’s assume the question was to look at some sales order processing lines for these particular accounts. Sales order processing doesn’t have the account number, but it does have the account index. I can get the account index based upon the list that finance gave me and the sales order table.

tsql shortcut

With these indices, I can create an “In” clause.

tsql shortcut

Since I’ve already copied the Account Index column from the bottom, I can now paste it up here in the “In” clause.

tsql shortcut

Close the parentheses at the end of the last number, and then move the cursor to the beginning of the line for the last index in the list. We need to put in a separator, so we will once again hold the shift and the alt keys and go all the way up to the second number from the top, highlighting those rows, and add a comma, to all rows at once.

tsql shortcut

Now I can quickly select everything from that table where that index is (as shown above).

This particular example is for demonstration purposes only.  We could have certainly joined SOP Lines to the GL table, but I wanted a simple example to show this tip.  We’re using this to show the shift-alt method of editing multiple lines. Of course, this trick can be used in multiple places, not just when it comes to editing multiple lines of TSQL code.  It may work in Word, Notepad, Outlook, etc…  It may also be version specific so if it does not work for you it may be an older version of SQL.

Have any questions about this tip or do you have your own tricks for editing TSQL? Please contact us and let us know!

This publication contains general information only and Sikich is not, by means of this publication, rendering accounting, business, financial, investment, legal, tax, or any other professional advice or services. This publication is not a substitute for such professional advice or services, nor should you use it as a basis for any decision, action or omission that may affect you or your business. Before making any decision, taking any action or omitting an action that may affect you or your business, you should consult a qualified professional advisor. You acknowledge that Sikich shall not be responsible for any loss sustained by you or any person who relies on this publication.

SIGN-UP FOR INSIGHTS

Join 14,000+ business executives and decision makers

Upcoming Events

Latest Insights

About The Author