Hot keys for queries in SSMS

Dedicated to those who are tired of writing the same SELECT

It would seem, what nonsense, but there are no quick and intelligible answers in googlenets, so I will give them here.

  1. In SQL Server Management Studio, it is not possible to assign a hotkey to a snippet.

  2. I couldn’t find anything Emmet-like. If you find it, post it in the comments.

  3. Can be finished with a file.

SSMS has snippets. There are two ways to get to them:

  1. Menu Tools (Services) -\u003e Code Snippets Manager (Manager of code snippets), it is also Ctrl + K, Ctrl + B. It shows us a list of folders with snippets, going into which you can actually see the snippets themselves.

  2. Context menu

The most interesting is in the context menu. There are two whole points: Insert Snippet (Insert a snippet of code) and Surround With (Place in a fragment: Ctrl + K, Ctrl + S). The first one shows the same list of folders, and the second one shows a certain limited set of snippets that are called by clicking on them.

The fact is that SSMS has two types of snippets: Expansion and SurroundsWith. And it is the second ones that are displayed in the Surround With context menu item. Snippets XML files are stored in the folder specified in the Code Snippets Manager.

The drop-down list of the Surround With context item (Ctrl+K, Ctrl+S) picks up all snippets from the subfolders of this folder that have the SurroundsWith type. The type is defined in the node SnippetType in the snippet code itself:

	<Description>Фрагмент кода для конструкции If.</Description>
	<Author>Корпорация Майкрософт</Author>

Next, there is a Code node, which defines what actual text should be inserted.

An example of my elementary SELECT snippet

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="">
	<_locDefinition xmlns="urn:locstudio">
		<_locDefault _loc="locNone"/>
		<_locTag _loc="locData">Title</_locTag>
		<_locTag _loc="locData">Description</_locTag>
		<_locTag _loc="locData">Author</_locTag>
		<_locTag _loc="locData">ToolTip</_locTag>
	<CodeSnippet Format="1.0.0">
			<Code Language="SQL">
from (nolock)
where id = 

The syntax also allows you to pass parameters, make some placeholders, on which, immediately after inserting, you can run through the tab and fill in the necessary places, but you can read about all this in the official dock.

To call, you need to make as many as four clicks on the keyboard, but this is better than nothing, and they are quickly brought to automatism: Ctrl + K, without releasing Ctrl, press S, down arrow, Enter.

To prevent the default SurroundsWith from getting into the list, they must be deleted or their type changed. Saves a lot of nerves and time when you write dozens of selects a day. Sometimes you don’t even feel like writing them, because you have to write again. Use on health!

Similar Posts

Leave a Reply Cancel reply