SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Creating Snippets


Creating Snippets

Author
Message
Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)

Group: General Forum Members
Points: 162559 Visits: 22753
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
Brandie Tarvin
Brandie Tarvin
SSC Guru
SSC Guru (155K reputation)SSC Guru (155K reputation)SSC Guru (155K reputation)SSC Guru (155K reputation)SSC Guru (155K reputation)SSC Guru (155K reputation)SSC Guru (155K reputation)SSC Guru (155K reputation)

Group: General Forum Members
Points: 155551 Visits: 9808
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/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.
Steve Jones
Steve Jones
SSC Guru
SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)

Group: Administrators
Points: 603852 Visits: 21112
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

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)

Group: General Forum Members
Points: 162559 Visits: 22753
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
Steve Jones
Steve Jones
SSC Guru
SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)

Group: Administrators
Points: 603852 Visits: 21112
bump

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)

Group: General Forum Members
Points: 162559 Visits: 22753
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
Steve Jones
Steve Jones
SSC Guru
SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)

Group: Administrators
Points: 603852 Visits: 21112
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

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search