Introduction
Coders are used to hitting F5 while in a SQL editor window in SSMS to run their query. This has a very different effect when in a SQL window in Visual Studio, as in VS. Hitting F5 normally causes the project to build and run, and the query is ignored. This article describes how to make things work as we are used to when using SSMS to run queries.
Let's Do It
As if the command mapped to the F5 key in most browsers versus the command mapped to the F5 key in SQL Server Management Studio (SSMS) doesn’t cause me enough grief, Visual Studio brings more annoyance to the party with its two easily confused SQL windows, the Transact-SQL Editor and the Query Designer. I think the database management tools in Visual Studio 2010 are a vast improvement even on Visual Studio 2008, and I often find them more useful than just having SSMS running in a separate application window, but as a seasoned SSMS user, I expect results when I press F5, or Execute in SSMS. Hitting F5 in a data window in Visual Studio has only one result by default, and that is to start a build. Inadvertently triggering a build in Visual Studio doesn’t make for a shiny, pretty, Sunday School type situation, especially with large solutions and complex build processes, so I think my advice to remap the F5 key for data windows in Visual Studio, to rather execute the current SQL query, is pretty good advice. This is very easy to do once you know where to look, and that’s where I hope this show and tell post will be helpful to you. Let’s start with the Transact-SQL Editor. My favourite addition to the database tools in Visual Studio, this is the closest you get to the good old, free text query window in SSMS. I find the Query designer a schlep to open, as I always get subjected to the Add Table dialogue, and then still have to hide the noisome Diagram and Criteria panes before I can get hacking away at a cool query.
Let’s make F5 execute the SQL query in the active Transact-SQL Editor pane. Bring up the Options dialogue by clicking Tools->Options on the main Visual Studio menu. This will present you with the unfriendly and anachronistic spectre of a very densely populated dialogue that is both modal and fixed size. Hard to believe in this day and age, but it’s all we have. Here it is, together with numbered steps which I explain below:
Lets look at the numbers, as they say:
- Type the word execute into the Show Commands containing field. This really narrows down an otherwise punitively long list of commands. I initially tried using the term sql, but still that filtered list was even longer than anything you want to deal with in a retarded dialogue that can’t be resized and only shows about five commands at a time.
- There are only three commands in VS 2010, and I don’t know what
Data.Execute
does, but it doesn’t concern us here. Select the Data.SqlEditorExecuteSql
command to proceed. - Remove the existing key binding for this command.
- Limit the scope of our binding to the SQL Editor.
- Give the Press shortcut keys textbox focus and press F5. Don’t try and type F and 5.
- Assign the shortcut key to the
Data.SqlEditorExecuteSql
command. - After repeating this procedure for the
QueryDesigner.ExecuteSql
command, tidy up by closing the Options dialogue.
Now you can enjoy the peace of mind that whenever you are editing a SQL query, be it in SSMS or VSA 2010, hitting F5 will execute your query, which will hopefully be quicker that waiting for a build and getting taken against your will into a debugging session.
History
I published this on my blog, The Praxis, but I'm shifting the focus of that blog to discussion and theory, and will in future, publish most of my technical works here on CodeProject.