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

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.

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.

The usual Shift + Arrow or Shift + PgUp / PgDown that apply to most or all Windows apps...
Then I use Ctrl + E to execute

Highlight the query you want to run, then F5.

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.

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).

With SSMS 2012 you can try SSMS Executor add-in.
Default shortcut Ctrl-Shift-E.

I just mark the line I'm on (Shift + ↓ or Shift + End), and then click Alt + X.

In SSMS 2008R2 with line numbers displayed, you can double-click a line number anywhere adjacent to a query to select the entire query that's part of that line. It's a great feature and I use it all the time.
Unfortunately the feature was lost in the 2012 transition to the Visual Studio editor. I created a Connect issue for this, which I welcome votes on.

A new version of SSMS is in development by Microsoft (kind of new version) it's called SQL Operations Studio.
With SOS (haha) you can hit Ctrl+F5, or — like me — replace the shortcut to Ctrl+Enter

SQL Server Management Studio don't have the feature to execute single SQL statement from the SQL script editor. even we create different shortcut still it doesn't work as we want.
I found a alterative for SQL Server Management Studio => DBeaver
, community version is free.
Download DBeaver
Select SQL server option to connect
Download connector if not already installed(Auto suggest, popup will come)
Shortcut to execute SQL statement/Single line query -> Ctl + Enter
To execute SQL script -> Alt + X

Ctrl k + u will high lite text
f5 to run

Related

How do you debug or step through the code in SQL Server Management Studio 18?

SQL Server Management Studio used to have Debug functionality that would allow to step through the code and watch the values etc. Referring to How to add the Debug button to SSMS v18?, I understand that the functionality is removed from SQL Server Management Studio V18.1.
But what is the alternative now? How do you step through the code to pinpoint a bug in the code?
You can use Visual Studio (the full IDE) to do it:
Use "Server Explorer" (Under the View menu) to connect to your DB
Right-click the DB and choose "New Query" (or find a stored procedure to debug)
Set a break point (F9 key or otherwise) on a line of SQL
Right-click inside the SQL editor and choose "Execute with Debugger"
Your best option is to not upgrade version 18.X, but stay on 17.9 until the Microsoft Product Manager gets sacked. A similar thing happened in going from SQL Server 2000 to SQL Server 2k5. Eventually, they capitulated and re-introduced the feature.
Ta for answer above, but when trying to debug with Sever Explorer, I find that "SQL Debugging is not supported on Microsoft Azure SQL Database'... ho hum, so I downgrade to SSMS 17.9 or dump the database to local and do it there.
Nice, I just wanted to quickly debug something... by the time I've done that lot I'll have forgotten what the problem I was trying to fix was !
It's deprecated, so you don't. Personally, I never use the feature and I'm assuming many others didn't either (which is why it's being retired).
The work around is to debug stored procedures by outputting values using PRINT or RAISERROR. Using BEGIN TRAN / ROLLBACK this works well because you can easily run code against the same data.
It's not the same as step through debugging, but it works.

SQL Server Management Studio: F4 equivalent Toad shortcut

In Toad for Oracle you can put the cursor on the name of a object and press F4 to open it's definition.
Is there anything similar in T-SQL / SQL Server Management Studio ides?
I have developed SSMSBoost add-in for SQL Server Management Studio and it does exactly what you want. The shortcut is F2
If you want the definition of an object, the procedure sp_helptext will give that to you. As for assigning it to a keyboard shortcut in SSMS, you can do so by going to Tools → Options → Keyboard → Query Shortcuts and assigning sp_helptext to one of the available slots. Once that's done, highlight the name of an object in your query editor window, hit the assigned shortcut and it should display the definition. As someone mentioned above, sp_help will get you different information (i.e. columns, indexes, constraints, etc) if the object is a table or a view. Luckily, the same process applies to sp_help!

How to open SQL Server Management Studio with pre-defined Server + Database selected + new blank Query open

I use thousand times the SQL Server Management Studio during the day and I just found annoying
every time I open it to select the server, select the database and press new query...
I'm using windows integrated authentication
I would like to find a way to automatically set those up
I'm using the Studio from 2005
I'm not looking for "Why don't you keep it open instead" answer, please...
any tips?
Try this (I'm using SSMS 2008):
Open properties window for your user and then change default database value to your desired one.
Go to Tools/options/Environment/General and select "Open new query window" inside "On startup"

CTRL + K, CTRL + F (Format Code) Not Working in MS SQL Server

The format selection command isn't working in SQL server management studio 2008 for me and I'm wondering if anyone might know how to get it working. I don't know if it was ever working.
Do you have any add-ins installed? For me Ctrl+K, Ctrl+F brings up the SSMS Tools Pack find dialog. So perhaps this is happening because an add-in has taken over the keyboard combo.
Also, what is your keyboard scheme under Tools > Options > Environment > Keyboard? If this has been switched to "SQL Server 2000" you might try switching it back to Standard.
Finally, I believe there are cases where the keyboard shortcuts can get messed up depending on the co-existence and order of install for Visual Studio / BIDS etc.
IN SQL SERVER 2014
First do: Ctrl +K
Then: Ctrl+ Y
I know this post is really old, but for people searching for answers to this question I have this to add:
The Ctrl+K, Ctrl+F key combination for format code comes from poorsql.com. They have a SSMS/Visual Studio plugin. As of this date they currently support up to SSMS 2017.
This feature isn't available on SQL Server Management Studio.
There are various add-on's you can use to do this. I personally use ApexSQL. It gives you the option to create your own format template, so that your code is always easily readable for you.
Check this link:
http://www.mssqltips.com/sqlservertip/2413/sql-server-management-studio-keyboard-shortcuts-part-1-of-2/
...You can select specific lines and hit CTRL+K
followed by CTRL+C to comment it out and
CTRL+K followed by CTRL+U
to uncomment it out...

Run SQL script inside Visual Studio

I have a scripted database SQL file that I need to execute inside Visual Studio to create a new database. I am so far unable to locate any function that allows me to do this.
Go to server explorer in VS, right click on server and select run query.
Do you mind telling us why it has to be run inside VS?
In addition, the database projects offers a lot of features that are worth looking at:
http://www.informit.com/articles/article.aspx?p=31764 //old article but still worth reading.
http://www.ssw.com.au/ssw/standards/BetterSoftwareSuggestions/Images/VisualStudio_DataDude_SchemaCompare.gif
http://www.emadibrahim.com/2008/07/10/database-schema-compare-upgrade/
There is a green arrow of play on the upper left corner of the window query. And down the tabs of the names of open files in VS. Just click to play and ready.

Resources