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.
In SQL Server Management Studio, it is not possible to assign a hotkey to a snippet.
I couldn’t find anything Emmet-like. If you find it, post it in the comments.
Can be finished with a file.
SSMS has snippets. There are two ways to get to them:
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.
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:
... <Header> <Title>If</Title> <Shortcut/> <Description>Фрагмент кода для конструкции If.</Description> <Author>Корпорация Майкрософт</Author> <SnippetTypes> <SnippetType>SurroundsWith</SnippetType> </SnippetTypes> </Header> ...
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="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <_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> </_locDefinition> <CodeSnippet Format="1.0.0"> <Header> <Title>SELECT</Title> <Shortcut/> <Description/> <Author/> <SnippetTypes> <SnippetType>SurroundsWith</SnippetType> </SnippetTypes> </Header> <Snippet> <Code Language="SQL"> <![CDATA[select * from (nolock) where id = ]]></Code> </Snippet> </CodeSnippet> </CodeSnippets>
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!