Using Code Snippets in SSMS

  • I've been a huge fan of Snippets for sometime, and I have a bunch of them in a custom folder that I added to my SSMS 2008R2/2012/2014..

    However, since I started using SSMS 2016, I'm finding that I have to go into Code Snippets Manager and add my Folder every time.

    Has anyone else come across this? or know a way around it?

    thanks,

    Craig

    [font="Tahoma"]"Good is just the beginning" :-)[/font]

  • Have you tried directly adding your folders under:

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

    I would also assume you'd need to include the directory in the XML located in the same directory. Something like:

    <SnippetDir>

    <OnOff>On</OnOff>

    <Installed>true</Installed>

    <Locale>LOCALE_ID</Locale>

    <DirPath>%InstallRoot%\VSTSDB\Snippets\%LCID%\{Your Folder Name}\</DirPath>

    <LocalizedName>View</LocalizedName>

    </SnippetDir>

  • I use my own snippets since abt. three years now and I can't imagine what a waste a time it would be to work without them. Its frustrating enough to add them on other machines (via a share wherever it is possible) when I can't use my local SSMS.

  • SQLvis - Monday, December 19, 2016 7:43 PM

    Have you tried directly adding your folders under:C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio\SQL\Snippets\1033I would also assume you'd need to include the directory in the XML located in the same directory. Something like:<SnippetDir> <OnOff>On</OnOff> <Installed>true</Installed> <Locale>LOCALE_ID</Locale> <DirPath>%InstallRoot%\VSTSDB\Snippets\%LCID%\{Your Folder Name}\</DirPath> <LocalizedName>View</LocalizedName> </SnippetDir>

    9 mo's old but I just saw this...
    No change is necessary to the snippet.

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • Thanks, nice article. it is always helpful to me.
    I keep my mostly used queries in the folder in "Template Browser", that is easier way to access.

  • Thanks Avi1 that is alot easier than messing around with snippet folders.

  • ApexSQL complete offers THE best support for code snippets. It has support for creating code snippets directly in SQL (without need to mess around with XML), bind them to keywords and include them in suggestions while you're typing. And it's free.

  • Luis, we put ours in a shared folder on the common drive and then have everyone point their Code Snippet Manager there, so that if someone adds a new one we all get the use of it. I'm not sure how many team members are actually taking advantage of it, but I do regularly. I've even used it to enforce the documentation standards, by creating  a snippet for blank scripts/SPs that includes most of the things we look for in QA, and some commented-out reminders for things like adding extended properties and unit tests.

    Also snippets for things like querying all of the servers we use regularly, so you don't have to remember full names, etc.

    You can do a lot with snippets very fast, if you put some thought into how to use them best for your situation.

    -------------------------------------------------------------------------------------------------------------------------------------
    Please follow Best Practices For Posting On Forums to receive quicker and higher quality responses

  • bojned - Friday, August 17, 2018 4:15 AM

    ApexSQL complete offers THE best support for code snippets. It has support for creating code snippets directly in SQL (without need to mess around with XML), bind them to keywords and include them in suggestions while you're typing. And it's free.

    Thank you for sharing. I used to have ApexSQL software in my previous job and it worked nicely. However, this solution is for those poor guys that aren't able to install "unauthorized" software in their machines.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • jonathan.crawford - Friday, August 17, 2018 6:49 AM

    Luis, we put ours in a shared folder on the common drive and then have everyone point their Code Snippet Manager there, so that if someone adds a new one we all get the use of it. I'm not sure how many team members are actually taking advantage of it, but I do regularly. I've even used it to enforce the documentation standards, by creating  a snippet for blank scripts/SPs that includes most of the things we look for in QA, and some commented-out reminders for things like adding extended properties and unit tests.

    Also snippets for things like querying all of the servers we use regularly, so you don't have to remember full names, etc.

    You can do a lot with snippets very fast, if you put some thought into how to use them best for your situation.

    That's a great idea. It's great to learn about teams working together and sharing tools.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Great article thanks!!
    In case someone wants to share thos esnippets, here's a tool can could be handy: https://www.snip2code.com
    It is a snippet repository with more than 3 millions of snippets created by the community.

  • Hi
    Very good article.
    Immediatly after reading it i tried creating my fist snippet that I dreamt of for years:
    An "Encapsulate" snippet.
    So my first snippet was SUM as in SUM(field)
    Great
    Then my ballon exploded when I tried to create my second SUMAS snippet. Boo boo boo. Cannot be done.
    I wanted a SUM(field) AS field snippet. SSMS does not let you put $selected$ twice in the snipet. So that kind of encapsulation snippet cannot be accomplished.
    I would have loved to create SUMAS, ISNULLAS, CONVERTAS snippet. Cannot be done.
    It is a shame that Microsoft does not let you put $selected$ more than once in a snippet.

    But still ...it is very usefulle for other use
    thanks

  • mtrudel337 - Monday, August 20, 2018 6:39 AM

    Hi
    Very good article.
    Immediatly after reading it i tried creating my fist snippet that I dreamt of for years:
    An "Encapsulate" snippet.
    So my first snippet was SUM as in SUM(field)
    Great
    Then my ballon exploded when I tried to create my second SUMAS snippet. Boo boo boo. Cannot be done.
    I wanted a SUM(field) AS field snippet. SSMS does not let you put $selected$ twice in the snipet. So that kind of encapsulation snippet cannot be accomplished.
    I would have loved to create SUMAS, ISNULLAS, CONVERTAS snippet. Cannot be done.
    It is a shame that Microsoft does not let you put $selected$ more than once in a snippet.

    But still ...it is very usefulle for other use
    thanks

    maybe try using $selected$ as a string literal in the declarations, then use that variable instead

    -------------------------------------------------------------------------------------------------------------------------------------
    Please follow Best Practices For Posting On Forums to receive quicker and higher quality responses

  • mtrudel337 - Monday, August 20, 2018 6:39 AM

    Hi
    Very good article.
    Immediatly after reading it i tried creating my fist snippet that I dreamt of for years:
    An "Encapsulate" snippet.
    So my first snippet was SUM as in SUM(field)
    Great
    Then my ballon exploded when I tried to create my second SUMAS snippet. Boo boo boo. Cannot be done.
    I wanted a SUM(field) AS field snippet. SSMS does not let you put $selected$ twice in the snipet. So that kind of encapsulation snippet cannot be accomplished.
    I would have loved to create SUMAS, ISNULLAS, CONVERTAS snippet. Cannot be done.
    It is a shame that Microsoft does not let you put $selected$ more than once in a snippet.

    But still ...it is very usefulle for other use
    thanks

    You could change your approach. Instead of using a Surround With snippet, use an Expansion one. If you already have the column, copy or cut it, insert the snippet while replacing the column and paste the column on the literal. Something like this:

    <?xml version="1.0" encoding="utf-8" ?>
    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
        <CodeSnippet Format="1.0.0">
            <Header>
                <Title>SUM AS</Title>
                <Shortcut></Shortcut>
                <Description>Add SUM() and alias to a column</Description>
                <Author>Luis C</Author>
                <SnippetTypes>
                    <SnippetType>Expansion</SnippetType>
                </SnippetTypes>
            </Header>
            <Snippet>
                <Declarations>
                    <Literal>
                        <ID>ColumnName</ID>
                        <ToolTip>Name of the column</ToolTip>
                        <Default>ColumnName</Default>
                    </Literal>
                </Declarations>
                <Code Language="SQL">
                    <![CDATA[SUM( $ColumnName$) AS $ColumnName$]]>
                </Code>
            </Snippet>
        </CodeSnippet>
    </CodeSnippets>

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Need the extra step of doing a copy-text first but works perfectly
    Thank you

Viewing 15 posts - 16 through 30 (of 34 total)

You must be logged in to reply to this topic. Login to reply