In SQL Server Management Studio, you are able to define custom query shortcuts to help with common tasks. I have been using them for quite some years now and realized I haven’t written about them yet. So here we go.
The query shortcuts can be found under
Tools -> Options... -> Environment -> Keyboard -> Query Shortcuts and the default setting doesn’t look very exciting.
Before I go any further, let me explain how query shortcuts work. The “shortcut” is obviously the key combo you press which does two things. It (1) prepends the “statement” to the highlighted text in the editor and (2) executes it. Although SSMS calls the statement Stored Procedure but it is not limited to only procs, it works with any valid T-SQL statement. In other words:
shortcut = statement [highlighted text]
Let me show you with an example, let’s say you have a query that looks like the one below and you would like to run
sp_help sys.columns to see the table details:
select * from sys.objects a join sys.columns b on a.object_id = b.object_id
Without a shortcut, you’d need to go to a new line and type the command. Now with the help of the shortcut, you can simply select the text
sys.columns and press
Alt+F1 to achieve the same result. How cool is that? I hear you want more. Ok, I will reveal all the query shortcuts I find particular useful.
sp_helptext – How many times have you looked at a view or proc and wondered about the implementation underneath. Well, no need to search for that object in Object Explorer anymore, simply setup a shortcut. My personal preference for this shortcut is the
select * from sys.objects where name like – This is quite handy when you want to quickly check to see if an object exists in the database. I normally set this to
select – Don’t let the simplicity of this shortcut fool you. Not having to type those six characters can save a lot of time when you want to run a system function embedded in a query, such as suser_sname(), newid(), getutcdate() etc. I normally set this to
select top 10 * from – The reason for having a separate top 10 query shortcut from the select all is that it provides a quick and easy way of retrieving the column names with some sample data. I normally set this to
select * from – I use
Ctrl+0 for this.
The shortcuts listed above are the common ones I use almost everywhere whereas the ones below are less common but still handy to have around:
select concat(””, newid(), ””) as [newid()] – If you have a need to generate GUIDs all the time, then surely it deserves a shortcut slot.
set statistics io, time on – This one is for folks looking at performance.
select * from dbo.dbversion order by id desc – This is useful if you have a version table in the database. Setting up a shortcut can make it easier for checking versions.