How can I execute a single query in SQL Server Management Studio?

68,684

Solution 1

I did some searching and was unable to find any keyboard shortcuts for executing individual lines in SSMS. There is a reference online though.

http://msdn.microsoft.com/en-us/library/ms174205.aspx

Personally I just highlight the row in question (Shift + End or Shift + , etc.) and hit F5.

Ctrl + E works just as well on a Mac.

Solution 2

The usual Shift + Arrow or Shift + PgUp / PgDown that apply to most or all Windows apps...

Then I use Ctrl + E to execute

Solution 3

Highlight the query you want to run, then F5.

Solution 4

Since this was originally asked, DevArt has added this functionality to their SSMS add-on . After installation, press Ctrl-Shift-E to execute the statement block under the cursor.

Solution 5

This isn't exactly what the OP was after, but here are two related tips that might help:

SSMS 2008 had a handy feature where you can double click the left margin (where the line numbers are) and it will auto-expand your selection to the beginning and end of the query that contains that line. So, double-click, f5.

It also had a nice feature which was, iirc, alt+up/down or maybe alt+shift+up/down, which will extend your selection to the beginning and end of line for each line you move the cursor across, including the one you started on, regardless of where your cursor is on the line. So this is another handy way to select the query you want to execute.

SSMS 2012 did away with those shortcuts (maybe they exist, but they're not the SAME, by default).

Share:
68,684
sumeet
Author by

sumeet

Updated on August 13, 2020

Comments

  • sumeet
    sumeet almost 4 years

    Background: I'm using SQL Server Management Studio 2008 and I have a query window open that contains multiple queries.

    Is there a keyboard shortcut that will allow me to execute a single query without highlighting the query I would like to run first? Some other SQL tools have shorcuts that run the query that is under the cursor, which keeps the hands on the keyboard, and away from the mouse.

  • Dave Carlile
    Dave Carlile almost 15 years
    To clarify this a bit, you can use shift+uparrow, shift+downarrow to highlight the query you want, then Ctrl+E to execute it. No mouse involved at all.
  • sumeet
    sumeet almost 15 years
    Ah well, it will have to do. Thanks for take the time mmayo and everyone else!
  • Wouter
    Wouter almost 8 years
    OP: "without highlighting the query I would like to run first"
  • Wouter
    Wouter almost 8 years
    ctb is assuming line numbers are being displayed, this is the left margin being talked about. Since SSMS 2012 this feature has been lost. If you click (not double click) in the left margin (left from the line-numbers) you will toggle a breakpoint and make it look like the query is selected. Yet, when pressing F5, all the queries in the file will be executed!
  • Wouter
    Wouter almost 8 years
    There is a free version, and it works for 2012, all the way up to 2016. Thanks!
  • ctb
    ctb almost 8 years
    No. There are 2 areas in the left margin. On the far left, there's a gray area for breakpoints, and immediately to the right of that, a white area where you can click and drag to highlight lines. Every code editor I know of does this. Yes, the white area gets bigger if you show line numbers, but it's still there even when they're off (colors could change, depending on theme).
  • Wouter
    Wouter almost 8 years
    Ah, thx for pointing that out. Still... it no longer works in 2016... too bad. Hope they fix it soon...
  • jpaugh
    jpaugh over 7 years
    Nice to know, but when your hands are already on the keyboard, reaching for the mouse is much slower than End,Shift+Home; or End, Shift+Up + Up + Up.
  • Robotronx
    Robotronx over 5 years
    Worked like a charm.
  • Code Novice
    Code Novice almost 5 years
    Moving from Oracle after using SQL Developer for so long... this is a nuisance. I have to highlight every single query so I can run it without running all of the other queries I have on a single page?!?!... why does the semi-colon not allow me to run each query individually? The semi-colon should be the stop point imho. As in don't run queries after the semi-colon.
  • Manngo
    Manngo over 2 years
    @jpaugh You think so?