Quick Tips–SSMS Select a vertical block

I saw this years ago in a presentation from Aaron Bertrand. At the time I thought it was super cool and I’d use it all the time, but I haven’t found many uses. However since I needed to do this recently, this helped.

Imagine that you have this:

blockselect_a

A normal select statement. Perhaps you’ve qualified columns with SQL Prompt, or you’ve used some tool to enter this (or you’re a typing masochist). Now you add an alias for the column because you don’t want to type the full name everywhere. That causes SSMS to complain.

blockselect_b

You can’t run this because once you use an alias, you need to use it elsewhere. The full table name isn’t valid anymore.

Now you could do a search and replace (CTRL+H), but that presents other problems, not the least of which is replacing the table in the FROM name. Unless you want to go through and approve or deny every replacement. You could also edit Person to “p” on each line manually.

Hey, this is programming, we don’t do things over and over when we can avoid them.

Enter Block Select

If you place your cursor here, shown with the arrow as my capture tool missed it.

blockselect_c

Now I can click ALT+Shift and hold them down while I move my cursor to the lower right of the block I want to select. In this case, it’s between the “n” and period on the last line of the column list, above the FROM clause. Look at the image below.

blockselect_d

I’ve now selected a block, and I can hit delete. This gives me:

blockselect_e

Notice that my selection is a thin cursor still visible. I can actually type here. Imagine I typed “sn” now. This is what I’d get.

blockselect_f

I fixed the alias before I shot this, and once I moved the cursor, I lost my selection, but a simple ALT+Shift, lets me highlight, select, and type in a vertical block.

Handy when trying to correct a number of items on separate lines.

About way0utwest

Editor, SQLServerCentral
This entry was posted in Blog and tagged , , , . Bookmark the permalink.