18

I'm using SSMS 2014. I am able to insert a snippet but they don't seem to respond to shortcuts (ie crproc[tab])

Is this feature known to work?

<?xml version="1.0" encoding="utf-8" ?>
 <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
 <CodeSnippet Format="1.0.0">
 <Header>
 <Title>prod1</Title>
 <Description>testing</Description>
 <Author> dale </Author>
 <SnippetTypes>
 <SnippetType>Expansion</SnippetType>
 </SnippetTypes>
 <Shortcut>crproc</Shortcut>
 </Header>
 <Snippet>
 <Declarations>
 <Literal>
 <ID>DatabaseName</ID>
 <ToolTip>Name of database.</ToolTip>
 <Default>DB</Default>
 </Literal>
 </Declarations>
 <Code Language="SQL">
 <![CDATA[
 ------------------------------------------------------
 -- FILENAME: Template-Expansion.snippet
 -- PURPOSE: Template for Expansion (Insert) snippets.
 -- AUTHOR: Ken O. Bonn
 -- DATE: May 15, 2013
 ------------------------------------------------------
 SET NOCOUNT ON;

 EXEC SP_HELPDB $DatabaseName$;

 ]]>
 </Code>
 </Snippet>
 </CodeSnippet>
 </CodeSnippets>
recursive_acronym
  • 2,981
  • 6
  • 40
  • 59

4 Answers4

18

No, for some reason Microsoft seem to have no interest in actually implementing this feature, despite code snippets being totally useless without it (because of the number of mouse clicks and key presses you need to insert a snippet).

How many times a day do you type "SELECT * FROM..." or "SELECT TOP 10 * FROM "... etc..??! Very frustrating!

Even SSMS 2016 Release Candidate still does not support it.

"ApexSQL Complete" (a free tool) apparently will do it, but this does not support SSMS 2016 yet!

Nick D
  • 181
  • 5
6

Add your snippet to the "Functions" folder where other SurroundsWith snippets are found. Then press press CTRL + K, CTRL + S (shortcut for SurroundsWith Snippets) and type a few letters from your snippet name and press enter. If only the "Functions" folder has SurroundsWith snippets then no other folders will show in the context menu. Otherwise you can type a few letters from the appropriate folder name and press tab. This process is much like this answer.

For my environment the Begin, If and While snippets are located:

C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio\SQL\Snippets\1033\Function

Here is an example snippet for SELECT * FROM:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Select</Title>
            <Shortcut>sel</Shortcut>
            <Description>SELECT * FROM _ WHERE 1=1 AND</Description>
            <Author/>
            <SnippetTypes>
                <SnippetType>SurroundsWith</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Code Language="SQL">
                <![CDATA[
SELECT * FROM dbo.$selected$$end$
WHERE 1=1
--AND
]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
Community
  • 1
  • 1
Hans Vonn
  • 3,949
  • 3
  • 21
  • 15
  • OP asked if shortcut feature works in SSMS and your information is irrelevant. – Saulius Nov 17 '17 at 09:00
  • 2
    @Saulius I apologize my post is a little lengthy. If you follow it through it shows you how you can get shortcuts to work. Basically you are inserting the shortcut into the SurroundsWith snippets, and then using CTRL + K, CTRL + S and the shortcut you specified to quickly invoke it. – Hans Vonn Nov 17 '17 at 12:55
  • This is only a workaround. You still need to do 2 steps (first CTRL + K, CTRL + S and only then the shortcut). – stomy Sep 11 '19 at 14:40
  • 1
    I appreciate this workaround. CTRL+K, CTRL+S, S, ENTER or TAB will be the minimum number of keys. Just a note that the usage of `sel` in the example does nothing. – Larryjl Jan 04 '21 at 23:46
  • @Larryjl Thank you for the feedback. I have since forgotten/never learned what each element does. Feel free to update the answer if that is more valuable or remove the node if it has no purpose. – Hans Vonn Jan 13 '21 at 18:59
3

In ApexSQL Complete you have possibility to do this and of course that support SSMS 2016 and even new SSMS 17 RC, except that it is totally free

Kimi
  • 51
  • 1
  • 3
0

Don't worry about giving it a specific shortcut.
Just add it under the my snippets folder.

Make sure to save as .snippet

Hit ctrl+K, Ctrl+S
down arrow to my snippets enter
down arrow to the snippet you want enter

your snippet will surround the code you wanted it to.