SQL Server Management studio provides a very useful functionality called query shortcuts. This makes the task of querying the sql server easier. Just by pressing two keys on the keyboard a complex query can be executed.

Lets take a scenario where a query is very frequently used.

Lets suppose the query to be
[sql]
SELECT top 10 * FROM PRODUCTS
[/sql]

Now instead of writing the query every time and executing it a query short cut can be setup for this query.

Steps to setup query shortcut in SSMS in detail:

1) Select tools menu and then select options

2) In the options expand environment and then click on Keyboard.

3) In the query shortcuts plane give the query in the Stored Procedures section for one of the shortcuts.

query shortcuts
query shortcuts

4) Click on OK.

Now in order to execute the query using shortcut click any where inside the query window and press ctrl+[shortcutnumber] and the query gets executed. Simple!!!

As shown in the screen shot above the shortcut is ctrl+3.

Lets see a more advanced scenario where the query would be
[sql]
SELECT top 10 * FROM PRODUCTS where productid = 100
[/sql]

In order to execute this query using shortcut just type
[sql]
where productid = 100
[/sql]

and select the text and press ctrl+3. Do not forget to add space before the where clause.

Done!.

In order to simplify this we can add the below query as shortcut.
[sql]
SELECT top 10 * FROM PRODUCTS where productid =
[/sql]

Now to find top 10 products that have productid as 100 just type 100 select it and press ctrl+3.

Note:
When a new query shortcut is created you have to open a new query in order to use it.
All the open query windows will not have the updated query shortcuts.

Leave a Reply

Your email address will not be published. Required fields are marked *