How can I run just the statement my cursor is on in SQL Server Management Studio?

Viewed 85397

As a long time Toad for Oracle user, I have gotten used to hitting Ctrl+Enter and having just the statement under the cursor be executed.

In SQL Server Management Studio, hitting F5 runs the entire script. To run just the current statement, I have to manually highlight the statement I want, and then hit F5.

That is really annoying to me. Does anyone know of a tool with a keyboard shortcut to run just the current statement on a SQL Server? I would change tools just for this one feature.

Note: Oddly enough, even the free Toad for SQL Server does not let you run just the statement under the cursor.

25 Answers

Use Ctrl+KU to select a line. Then use F5 to run it.

Although it only works for single line selection, still I find it quite useful.

Hope it helps!!

I used this work around; when code is not commented out

Ctrl + K + U

(this command highlight current query line) and then

Ctrl + E

(this run highlighted query).

It is useful when you want to run a single line query between some other lines like :

  Select top 100 * from [dbo].[Order]

  Select top 100 * from [dbo].[OrderItem]

  Select top 100 * from [dbo].[OrderStatus]

In Toad for SQL Server the following default hot-keys can be used for execution:

  • F5: Execute all SQL statements in editor
  • F9: Execute SQL statement at current cursor position
  • Shift-F9: Execute all SQL statements from cursor, including the current at cursor position

However, like 'ercan' wrote, you need to seperate/follow each statement with 'GO'.

SELECT TOP 5 * FROM accounts
GO

SELECT TOP 5 * FROM users
GO

SELECT TOP 5 * FROM contracts
GO

Easiest way to do this is assignment for CTRL + Enter combination for action Query.Execute in SQL Server Management Studio.

  1. Open Tools > Options menu.
  2. Select Environment > Keyboard page in left pane.
  3. Find Query.Execute action and select it.
  4. Set SQL Query Editor in "Use new shortcut in:" dropdown list.
  5. Now type CTRL + Enter combination in press shortcut keys: textbox.
  6. Click the Assign button.
  7. Change Shortcut currently used by: to Query.Execute (Ctrl+Enter (SQL Query Editor))
  8. Click OK. Done.

If the executor add-on solution (in the top posted answer) isn't working properly, i got it to work for me (SSMS v17.8.1): The add-on adds a command under tools: Tools > Execute Inner Statement.

You can assign a custom keyboard shortcut key to it by going to Tools > Options > Keyboard then search for "execute" in the 'Show commands containing:' and selecting Tools.ExecuteInnerStatement. Then just assign your desired keystroke(s) in the 'Press shortcut keys:' field and hit the Assign button.

This feature is present in SSMSBoost add-in for SSMS (I am the developer of this add-in):

Shift-F5 will select the current statement (which allows you to review what you are currently going to execute). Then you press F5 and execute it.

Try to define macro in SSMSBoost SSMSBoost/Settings/Macros:

Select free slot for your macro in right panel fill caption (if you want) add 2 commands to sequence: SSMSBoost.SelectCurrentStatement Query.Execute assign youur favorite shortcut and that's all.

I agree with JosephStyons but can not upvote or comment. DBeaver, squirrel, PL/SQL Developer, Toad all have the option to execute a single statement.

In my case I use these other tools which all have the option but then when I us SQLServer Management Studio, I am at risk of inserting and/or deleting because I forgot to highlight the select statement.

This is not an option, it is a requirement. However Microsoft refuses to acknowledge logical useful features needed by users but chooses to cater to useless and cosmetic features.

UPDATE: I wonder if Microsoft and SQL Server users feel this bug is a "feature" because they are able to (forced to) chain SQL Statements together and all execute at once. Perhaps that is why the request fall on deaf ears?

Redgate's SQL Prompt extension for SSMS can do this.

It's not free but in my experience it's well worth having. Better intellisense than the SSMS out of the box version, tab colouring, tab history (so valuable!), snippets, single statement (not just a single line) execution, and lots more.

To run just a section of a larger script, try this ..

Steps:

  1. Collapse the SQL statement you want to run by clicking the minus sign in the left margin next to the statement. That will show only the first line (with an ellipsis "..." to indicate more code not shown) and the ending ";" for the statement.

  2. Highlight the line.

  3. Press [F5] key.

That will run just the highlighted statement. If you want to run more than the one statement at a time, collapse each statement and highlight all that you want to run, then click [F5]. It's a work-around, but still much easier than having to drag and highlight numerous lines of code every time.

Use Shift+ (arrow down) to select single row, save a button than Ctrl+KU :D lol.

Then use F5 to run it.

Hope it help too..

Related