Creating Snippets

  • I'm not sure if this is still useful and works the same for 2014 and 2016. I've created a few for myself on 2012 and seem like a useful tool. I couldn't find any article about this and only found this forum post http://www.sqlservercentral.com/Forums/Topic1465260-2799-1.aspx

    Would it be a good article or would it be irrelevant for the majority?

    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
  • I'm not quite sure what you mean by this, Luis. When I think of snippets, I'm thinking of code I reuse over and over, but generally I just save the code to my Management Studio solution and the open it / alter as needed.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • I use snippets in Prompt.

    They've been in SSMS as Templates for a long time. You can drag/drop them into query windows. CTRL+Shift+M for parameters

  • I've seen that Prompt handles snippets in a wonderful way. I know that templates have been available for a long time, but I'm not sure if you could create custom templates and not only the "create new object" that came included.

    Of course, a code used over and over could be saved as a script, but when the code is usually part of something bigger, then a snippet could be a better option. For example, I made the following for a cteTally which can be inserted by Ctrl+K, Ctrl+X and a few other keystrokes.

    <?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>

    <_locTag _loc="locData">Default</_locTag>

    </_locDefinition>

    <CodeSnippet Format="1.0.0">

    <Header>

    <Title>Tally CTE</Title>

    <Shortcut></Shortcut>

    <Description>Creates a cteTally</Description>

    <Author>Luis Cazares</Author>

    <SnippetTypes>

    <SnippetType>Expansion</SnippetType>

    </SnippetTypes>

    </Header>

    <Snippet>

    <Declarations>

    <Literal>

    <ID>cteTally</ID>

    <ToolTip>Code to create a cteTally</ToolTip>

    <Default>cteTally</Default>

    </Literal>

    </Declarations>

    <Code Language="SQL"><![CDATA[WITH

    E(n) AS(

    SELECT n FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0))E(n)

    ),

    E2(n) AS(

    SELECT a.n FROM E a, E b

    ),

    E4(n) AS(

    SELECT a.n FROM E2 a, E2 b

    ),

    cteTally(n) AS(

    SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) n

    FROM E4

    )

    SELECT n

    FROM cteTally

    ]]>

    </Code>

    </Snippet>

    </CodeSnippet>

    </CodeSnippets>

    Or the following that would help me to create a concatenated string from a query. This one is called by Ctrl+K, Ctrl+S as this surround the selected text.

    <?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>

    <_locTag _loc="locData">Default</_locTag>

    </_locDefinition>

    <CodeSnippet Format="1.0.0">

    <Header>

    <Title>FOR XML PATH</Title>

    <Shortcut></Shortcut>

    <Description>Creates a Concatenated list</Description>

    <Author>Luis Cazares</Author>

    <SnippetTypes>

    <SnippetType>SurroundsWith</SnippetType>

    </SnippetTypes>

    </Header>

    <Snippet>

    <Declarations>

    <Literal>

    <ID>FORXMLPATH</ID>

    <ToolTip>Code to create a Concatenated list</ToolTip>

    <Default>Concatenated list</Default>

    </Literal>

    </Declarations>

    <Code Language="SQL"><![CDATA[SELECT STUFF(( $end$$selected$

    FOR XML PATH(''), TYPE).value('.', 'varchar(max)'), 1, 1, '')

    ]]>

    </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
  • bump

  • Steve Jones - SSC Editor (2/9/2016)


    bump

    Does this means that an article on this could be interesting?

    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
  • Luis Cazares (2/10/2016)


    Steve Jones - SSC Editor (2/9/2016)


    bump

    Does this means that an article on this could be interesting?

    yes

Viewing 7 posts - 1 through 6 (of 6 total)

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