How can I run just the statement my cursor is on in SQL Server Management Studio?
Sql ServerDatabaseEditorSql Server Problem Overview
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.
Sql Server Solutions
Solution 1 - Sql Server
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!!
Solution 2 - Sql Server
You can check out this add-in for SSMS 2012. Place the cursor within the statement you want to execute and press CTRL+SHIFT+E
SSMS Executor - https://github.com/devvcat/ssms-executor/releases
Update:
Project moved to github and the addin re-written to support SSMS 2014, SSMS 2016. (Previously, the project lived on codeplex, at SSMS Executor - http://ssmsexecutor.codeplex.com/.)
Solution 3 - Sql Server
Ok, so what I'm getting from all these answers is "No, that is not possible."
Edit:
Here is how I was able to do this:
1 - Download SQL Developer
2 - Download the jTDS driver
3 - Follow these instructions to add that driver to SQL Developer
4 - Connect to SQL Server using SQL Developer (cool!)
5 - Run it and life is good
Solution 4 - Sql Server
Somebody suggested this features on Devart dbForge SQL Complete (addon for Management Studio) , and it's still in the development stage. Let's hope that it's finished and doesn't get abandoned halfway through development.
Solution 5 - Sql Server
> Note: Oddly enough, even the free Toad > for SQL Server does not let you run > just the statement under the cursor.
It is really annoying that TOAD does not hold to what it promises:
From TOAD help: [F9 to execute] a portion of a statement, which can contain one or more statements. You can select the portion of the statement by placing the cursor within or adjacent to the statement, or by selecting the statement. Note: Toad considers "adjacent" all statements (including comments) separated from the cursor or from each other by fewer than two blank lines. If an error occurs during statement execution, an error message displays, allowing you to either ignore the error and continue or to abort execution.
I tried it million times but it simply executes the whole script. I wanted to search for it in the user support group (toadss(at)yahoogroups.com) but yahoo has the stupidest search facility ever! It can't even find the keyword "toad" in the toad mail group, DOH!
I think TOAD is the best querying tool ever, but the lack of this feature really annoys me too.
UPDATE: SOLUTION FOUND! I asked this issue in toadss mail group and got the answer. Unlike Oracle, you have to separate statements in SQL Server with the keyword GO after each statement. Only if you do that the F9 button works as expected, executing the current statement.
Solution 6 - Sql Server
Above answer helped me to create a shortcut to Execute current Statement without selecting the query
1 - Click on Tools > Options > Environment > Keyboard
2 - For Show commands containing, set it to SqlComplete10.Common_ExecuteCurrentStatement
3 - For Use new shortcut in, set it to SQL Query Editor
4 - For Press shortcut keys(desired shortcut key), perform the Ctrl-Enter combination.
5 - Click on Assign. Click on OK.
Solution 7 - Sql Server
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]
Solution 8 - Sql Server
I use a workaround: I comment queries I'm not using. You can use CTRL-K, CTRL-C to comment the SQL you've highlighted. Use CTRL-K, CTRL-U to uncomment. That way, you can comment all other queries, and execute the one you're interested in with F5.
Solution 9 - Sql Server
I don't think this is possible to do using just the management studio. BUT you can use keyboard shortcut software (e.g. http://www.autohotkey.com/) to have a special sequence recorded and assigned to a hotkey. In your case you need:
<home><shift-end><F5>
This will select the current line and execute it.
Solution 10 - Sql Server
Hit Ctrl-E while text is highlighted.
Solution 11 - Sql Server
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
Solution 12 - Sql Server
Easiest way to do this is assignment for CTRL
+ Enter
combination for action Query.Execute in SQL Server Management Studio
.
- Open
Tools > Options
menu. - Select
Environment > Keyboard
page in left pane. - Find
Query.Execute
action and select it. - Set
SQL Query Editor
in"Use new shortcut in:"
dropdown list. - Now type
CTRL + Enter
combination inpress shortcut keys:
textbox. - Click the
Assign
button. - Change
Shortcut currently used by:
toQuery.Execute (Ctrl+Enter (SQL Query Editor))
- Click
OK
.Done
.
Solution 13 - Sql Server
If it's the having to move your hand to the mouse part that bothers you, you can hold the Ctrl down while hitting an up or down arrow key to select a line at once.
Solution 14 - Sql Server
SQL Complete Express edition has this built in. But you don't get it without the custom intellisense.
(I got this from Salamander2007's post)
Solution 15 - Sql Server
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.
Solution 16 - Sql Server
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.
Solution 17 - Sql Server
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.
Solution 18 - Sql Server
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?
Solution 19 - Sql Server
you could always use the command line tools sqlcmd and osql. I did a lot of sybase all at the unix command line using a wrapper function that passed my command string into the equivalent (which i think was isql?). I used vi to, so maybe I was just crazy then ;-)
Solution 20 - Sql Server
Just select (highlight) the single statement you want to run and hit F5.
Solution 21 - Sql Server
You can combine the two Tools as follow : SQL Complete from dbForce and AuhotHotkey.
With SQL Complete: you can execute the current statement at the cursor, by pressing the combination Ctrl Shift E
The reason that I used the AutoHotkey is to make life easier :-) by using just F6 to execute the current statement, by setting the following code in AutoHotkey script:
;SQL EXECUTE THE CURRENT STATEMENT
F6::
Send ^+E
return
And voilà you can execute any current statement at the cursor by just pressing F6
But sometimes, again my big problem is I mistakely press F5 instead of F6 :D
Solution 22 - Sql Server
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.
Solution 23 - Sql Server
To run just a section of a larger script, try this ..
Steps:
-
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.
-
Highlight the line.
-
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.
Solution 24 - Sql Server
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..
Solution 25 - Sql Server
The following works for me ... I use SSMS 2012
1 - Click on Tools > Options > Environment Keyboard
2 - For Show commands containing, set it to Query.Execute
3 - For Use new shortcut in, set it to SQL Query Editor
4 - For Press shortcut keys, perform the Ctrl-Enter combination.
5 - Click on Assign. Click on OK.