Introduction
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.
Using the Code
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 Tabledialogue, 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:
Let's 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 within 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.SqlEditorExecuteSqlcommand
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 and5.
- 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 than waiting for a build and getting taken against your will into a debugging session.
History
This tip was originally in my blog, but I am revising that blog to be less technical and I am moving all my technical articles here.