8

I am using SQL Server Management Studio 2016 on Windows 10. I'm tired of using shift/ctrl+up/down/right/left keys to select the code I want to run. I'm wondering whether there are shortcuts/snippets to select a block of code that separated from other code by blank lines?

Here is a code example:

select *
from tab1

select *
from tab2

select *
from tab3

Say, my cursor is inside the middle block and what's the best way to select the middle block?

  • 1
    JetBrains DataGrip (Ctrl + Enter). Cheap too. Dark UI too. – Nelz Mar 17 '17 at 13:36
4

I'm not affiliated to Red-Gate, but I would like to point out that if you hit Shift-F5, when you own a newish copy of SQL Prompt, then the query your cursor is on, will be marked green, and executed.

It has really saved me a lot of keystrokes.

| improve this answer | |
  • Thanks a lot for your answer! I tried sql prompty and it was amazing! I'll think about purchasing it after the trial! – Jason Mar 17 '17 at 14:27
  • 1
    Note that it doesn't actually select the code, inasmuch as you can't use this method to copy the code block that's been executed. – Thomas Rushton Mar 17 '17 at 14:28
4

Using Autohotkey, I was able to develop a solution to select a block of code and here is the script I have:

!b::

send ^f

sendraw ^\r$

send {F3}

send +{F3}

send {Esc}

send {down}

send ^{=}

return

Specifically, pressing Alt + B will invoke this script, and the script will select the block of code where the cursor is in. Note that the block of code has to be separated from other blocks of code by blank lines, and the block of code cannot have blank of lines inside itself.

The key steps of the about script are

  • using regular expression "^\r$ " to locate the blank lines above and below the cursor

  • moving the cursor to these two blank lines one by one

  • using ctrl + = to select the block of code between these two blank lines.

Note that !/^/+ are alt/ctrl/shift separately.

| improve this answer | |
  • I did not know that you could do this with AutoHotKey. A lot less espensive than SQL Prompt. Thank you for posting. – Henrik Staun Poulsen Mar 20 '17 at 16:16
3
+100

One way to quickly select blocks of code within SSMS is to minimize the code using the - button next to the leading keyword in a SQL statement (e.g. SELECT, UPDATE, DELETE, WITH, etc.) then highlight that line and press F5 to execute it.

enter image description here

Depending on your version of SSMS (newer versions obviously being preferred in this case), there are also some keyboard shortcuts you can use to collapse/expand a section. The keyboard shortcut link provided by Simon Hellings' earlier response alludes to hotkeys already being set, but I cannot confirm they actually work. However, you can set some custom hotkeys to do this as follows:

  1. The functions we're concerned with setting hotkeys for are found under the Edit -> Outlining Menu Options: enter image description here

  2. To set the hotkeys, navigate to Tools -> Options... -> Environment -> Keyboard -> Keyboard. Type in Outlin into the Show commands containing: box. Then assign a new shortcut key combinations for Edit.ToggleAllOutlining and Edit.ToggleOutliningExpansion and press Assign: enter image description here

  3. Now, check the shortcuts took after pressing Ok and your shortcut keys should work as expected. As you can see, I assigned mine to CTRL+ALT+Left Arrow and CTRL+ALT+Right Arrow, but you can use whatever you want: enter image description here

Finally if this doesn't work, you may have to switch to a different SQL client. It sounds like the functionality you want is similar to executing an Oracle PL/SQL block via CTRL + ENTER within Oracle SQL Developer. You can use Oracle SQL Developer to connect to SQL Server using Third Party Database JDBC drivers (ref), but I don't recommend it.

| improve this answer | |
  • Yes, this alone is reason enough to install Toad and use F9. Toad does have its own set of quirks though. – crokusek Mar 14 '17 at 1:26
  • Thanks for your reply! How can I fold the tree node without using mouse? – Jason Mar 14 '17 at 2:40
  • @Jason - I've updated my answer with the keyboard shortcuts. You'll need a newer version of SSMS, as this functionality wasn't available on all versions as indicated by the URL link I've provided. – John Eisbrener Mar 14 '17 at 13:40
  • @JohnEisbrener, thanks for your help and I'll check them out! – Jason Mar 15 '17 at 14:55
  • @JohnEisbrener, I was trying to test the new keyboard shortcut you suggested and none of them works for me. I'm using the newest version of SSMS, which is version 13.0.16106.4. Have you ever tested them by yourself? – Jason Mar 16 '17 at 23:47
2

Do not use the up/down/right/left keys at all to select your code to run.

Assuming that a computer mouse can be used, do this:

  1. Use your computer's mouse to move the cursor to the first character to be included.
  2. Next Hold down and continue holding down the Ctrl key, then move the mouse to include the last character to be included.
  3. Continue holding the Ctrl key, then press the E key which will execute the highlighted text.

If you want to delete the code just executed, you should Continue holding the Ctrl key and then press the Delete key.

I suppose it is possible that someone has created a tool to do that automatically.

| improve this answer | |
  • Thanks for your reply! I know how to use ctrl/shift with a mouse to select code and feel that's kind of slow comparing with shortkeys/snippets. Are you aware of any shortkeys/snippets that can select code automatically? – Jason Mar 11 '17 at 4:59
  • 4
    @RLF, I will award your answer with an upvote for successful trolling (intentional or not, doesn't matter.) – Daniel Hutmacher Mar 13 '17 at 11:02
1

I don't think that this is possible. See complete list of default out of the box shortcuts here. You may be able to customize something from the tools --> options menu

| improve this answer | |
  • Thanks for your answer! If shortkeys are not possible, are you aware of any snippets for this? – Jason Mar 11 '17 at 18:26
  • Not that I can think of – Simon Hellings Mar 12 '17 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.