Keyboard shortcut to automatically wrap selected text in IsNull([text], 0) in SSMS

前端 未结 2 1949
忘掉有多难
忘掉有多难 2021-01-22 07:42

Is there some way to have a shortcut in SQL Server Management Studio (SSMS) that when text is selected, it wraps that text in an IsNull() statement?

For example, I highl

相关标签:
2条回答
  • 2021-01-22 08:15

    I may have found a way. In the C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\SQL\Snippets\1033\Function directory (adjust accordingly for your installation), I copied the Begin End.snippet file, named the copy IsNull.snippet, and modified the new file. I changed:

    BEGIN
    
    $selected$ $end$
    
    END
    

    To:

    IsNull($selected$ $end$, 0)
    

    (I also changed the Title, Description, and Author tags -- do whatever you want with those.)

    Then, in SSMS, I used Tools -> Code Snippets Manager to import the new snippet. Now, with My_column_name selected in a query window, I can use Ctrl+K, Ctrl+S, Down, Down, Enter, Down, Enter to apply the new snippet.

    It's not a single keystroke, but it's heading in that direction. Maybe this will inspire someone else who can make it even better.

    0 讨论(0)
  • 2021-01-22 08:20

    You can get this done using snippets. It's not exactly a simple shortcut, but a few keystrokes will get you there.

    First you need to create a snippet 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>ISNULL</Title>
                <Shortcut></Shortcut>
                <Description>Inserts ISNULL function</Description>
                <Author>Luis Cazares</Author>
                <SnippetTypes>
                    <SnippetType>SurroundsWith</SnippetType>
                </SnippetTypes>
            </Header>
            <Snippet>
            <Declarations>
            </Declarations>
            <Code Language="SQL"><![CDATA[ISNULL( $end$$selected$, 0)]]>
            </Code>
            </Snippet>
        </CodeSnippet>
    </CodeSnippets>
    

    Save this as a .snippet file and save it on a folder destined for snippets. You could use the one set as default. To know which folder is your default or to add a different folder, go to the Code Snippets Manager in the Tools menu for SSMS.

    Once your snippet is on a registered location, select the code, press Ctrl+K,Ctrl+S and the snippets context menu will appear. You can traverse it by starting typing the folder and snippet name and pressing Enter or Right when getting there.

    It's probably not worth it for short snippets, but for larger ones it's amazing. I wrote a more detailed explanation in here.

    0 讨论(0)
提交回复
热议问题