Sql Code Snippets

Creating a custom snippet

Create a Snippets folder somewhere (I like c:SqlSnippets)

Sql01

Use the following template as your first snippet

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Logging - Standard Template</Title>
      <Shortcut></Shortcut>
      <Description>Insert script to read Logging</Description>
      <Author>Company Name Here</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="SQL">
        <![CDATA[
 SELECT
        DATEADD(hour, DATEDIFF(hour, GETUTCDATE(), GETDATE()), TimeStamp) AS RunTime
      , Message
      , UserName
      , . . .
 FROM Logging.dbo.LogTable WITH (NOLOCK)
WHERE TimeStamp > DateAdd(MINUTE, -30, GetUtcDate())
ORDER BY TimeStamp
]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Open Code Snippet Manager (^KB)

Sql02

  • Click Add button
  • Navigate to your new Snippets folder
  • Click the Select Folder button

Expand your Snippets Folder

Sql03

Cleaning up the Insert Snippet Dialog

Insert Snippet in Query window

  • Press ^KX to bring up the Insert snippet window (before our change)

Sql04

This dropdown is too busy and it is hard to find our custom snippets.

  • Go back to the Code Snippets Manager (^KB)

Sql05

  • Open the folder shown in the Location textbox.
  • Make a backup folder to save these scripts and move all files into the backup folder

Folder before backup

Sql06

Folder after backup

Sql07

Remove Missing Folders

Go back to the Code Snippets Manager and click the *Remove* button for all the missing folders. When you are done, it should look like this.
Sql08

Insert new Template

Finally, the payoff. Press ^KX to Insert a tempalte. It should be clean like this:
Sql09

Print Friendly, PDF & Email