SQL Server: re-run same lines - sql-server

The R language has this nice feature where you can re-run the same lines of code with CTRL+Shift+P. Does anyone know if that's possible with SQL Server Management Studio (SSMS)?
I find myself modify a bit of code and wanting to run it again, quickly. The highlight and execute is getting a bit repetitive. I would like a shortcut to run the same lines without the need to highlight.

Press F5 or Ctrl+E in SSMS. Both of these will run the highlighted SQL.
Hovering over the Execute button will have also displayed a tooltip to tell you this.

Related

Debugging SQL in SSMS v18.0+

I've just installed SSMS v18.6 and surprisingly I noticed that there's no debug menu anymore! I'm very dependent on debugging my SQL scripts like stored procs. Can anyone explain how I can achieve this? Any help would be much appreciated.
Debugging capabilities are no longer available in SSMS v18.0 onwards. I guess it's not been in high demand and also because of the overload it has imposed on the software. If you still need to debug your SQL scripts you can do it by using Visual Studio. There is a good video at How to debug SQL code showing you how to achieve your goal.
You can debug the SQL Server stored procedure and scripts with Visual Studio.NET,
Open the Visual Studio.NET and click the continue without code (link)
Connect to SQLServer in Visual Studio.NET. Menu Tools->SQL Server->New Query
Enter Sql Server Connection Data
In script note write your script or Invoke the Stored procedure or User define function
F9 or double click left of code line for out the break point
In menu SQL-> Execute with debugger
with F11 trace line By line
Complete

SSMS 2014 Only Allow Execute F5 if Text Selected

Update: Would like something that everyone can use, not just admin.
I tried to find if this question has already been asked before this post. In SSMS 2014, is there an option to only allow query execution if any text has been selected?
If none is selected, F5 and/or the execution button should be disabled. Thank you!
Based on my brief research, if user (like myself) who cannot run RAISERROR with severity level 20 or above, I am using SET NOEXEC ON and SET NOEXEC OFF in the very top and bottom. Then develop your script inbetween those two statements.
The downside is that there are two statements to add at 2 different locations. Also, if your script requires the use of SET NOEXEC, then this might not work.
I have this snippet that I sometimes include at the top of my queries:
raiserror('Highlight something and tell me what you''d like to do.', 20, -1) with log
Per the Microsoft Connect page titled, Add an Execute Selected Text Only to SQL Server Management Studio:
...We are considering adding this as a text-editor option for an
upcoming release. I have no way to mark it as "added to backlog", so
I'm resolving it as "Won't Fix". It's there though...
Cheers
-Sam Hughes
That was in 2011. If there is a way to do this I suspect it would be using Redgate. I'm not a redgate guru but I know that, for example, SQL Prompt will throw a warning before letting you do a DELETE or UPDATE without a WHERE clause. That's where I'd start.

IntelliSense duplicating part of table/field

I am using SQL Server 2008 R2 (10.50.4000). When I start typing the IntelliSense pops up and offers tables/columns. When I press tab to select and start typing further the IntelliSense adds characters. It is difficult to explain so hopefully the following screens will help:
I start typing:
I press TAB to select the table:
I press enter to go to next line to add my WHERE clause:
Has anyone seen anything like this before and if so can you please help.
Thanks
As suggested in the responses this was to do with RedGate SQL Prompt. I turned it off and everything works fine now. I am unsure why it only affects certain versions of SQL though.
Thanks all

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.

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

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

Resources