Search code examples
sqlsql-serveraliassql-scripts

Tired of typing SQL scripts like "SELECT TOP 10 * FROM" and other similar scripts. How to create an alias/shortcut in SSMS?


While doing a lot of research at my work I keep writing generic scripts like "SELECT TOP 10 * FROM" etc.

Is there a way to for me to write something like a snippet so that when i type "ss" + Space/Tab it will insert the "SELECT TOP 10 * FROM" script?

I have a number of scripts that I've noticed i use a lot and i would like to create some aliases for them...


Solution

  • While you could create a code snippet, it is tiresome to select the snippet.

    It still needs: CTRL-K CTRL-X M <tab> S <tab> to get:

    SELECT TOP 10 * FROM Table1 and being able to type the needed tablename.

    snippet:

    <?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>
    </_locDefinition>
        <CodeSnippet Format="1.0.0">
            <Header>
                <Title>Select</Title>
                            <Shortcut></Shortcut>
                <Description>SELECT TOP 10 * FROM ....</Description>
                <Author>?</Author>
                <SnippetTypes>
                    <SnippetType>Expansion</SnippetType>
                </SnippetTypes>
                <Shortcut>select</Shortcut>
            </Header>
            <Snippet>
                <Declarations>
                                    <Literal>
                                        <ID>TableName</ID>
                                        <ToolTip>Name of the table</ToolTip>
                                        <Default>Table1</Default>
                                    </Literal>
                </Declarations>
                <Code Language="SQL"><![CDATA[
    SELECT TOP 10 * FROM  $TableName$
    ]]>
                </Code>
            </Snippet>
        </CodeSnippet>
    </CodeSnippets>
    

    Saven this to (i.e.) "select.snippet", and Import in SSMS under 'Tools/Code Snipper Manager'

    It is unclear, to me, what the section <Shortcut>select</Shortcut> has for value ...

    According to next site, shortcuts is a "Won't Fix" back in 2013: https://dba.stackexchange.com/questions/166432/shortcuts-for-code-snippets