新增 Transact-SQL 程式碼片段Add Transact-SQL Snippets

本主題適用於: 是SQL Server是Azure SQL Database是Azure SQL 資料倉儲 是平行處理資料倉儲 THIS TOPIC APPLIES TO: yesSQL ServeryesAzure SQL DatabaseyesAzure SQL Data Warehouse yesParallel Data Warehouse

將您自己的 Transact-SQL 程式碼片段加入 [SQL Server]SQL Server中包含的預先定義程式碼片段集合。You can add your own Transact-SQL code snippets to the set of pre-defined snippets included in [SQL Server]SQL Server.

建立 Transact-SQL 程式碼片段檔案Creating a Transact-SQL Snippet File

建立 Transact-SQLTransact-SQL 程式碼片段的第一部分是使用您自己的程式碼片段文字建立 XML 檔案。The first part of creating a Transact-SQLTransact-SQL code snippet is to create an XML file with the text of your code snippet. 此檔案必須有 .snippet 副檔名,且必須符合 程式碼片段結構描述參考的需求。The file must have a .snippet file extension, and meet the requirements of the Code Snippets Schema. 將程式碼片段語言設定為 SQL。Set the snippet language to SQL.

您可以使用 [SQL Server]SQL Server 所隨附的預先定義程式碼片段當做範例。You can use the pre-defined snippets that ship with [SQL Server]SQL Server as examples. 若要尋找預先定義的程式碼片段,請開啟 Transact-SQLSQL Server Management Studio,選取 [工具] 功能表,然後按一下 [程式碼片段管理員]。To find the pre-defined snippets, open Transact-SQLSQL Server Management Studio, select the Tools menu, and click Code Snippet Manager. [語言] 清單方塊中選取 [SQL]Transact-SQLTransact-SQL 程式碼片段的路徑就會顯示在 [位置] 方塊中。Select SQL in the Language list box, the path to the Transact-SQLTransact-SQL snippets is displayed in the Location box.

註冊程式碼片段Registering the Code Snippet

在建立程式碼片段檔案之後,請使用程式碼片段管理員,向 Transact-SQLSQL Server Management Studio註冊程式碼片段。After creating the snippet file, use the Code Snippets Manager to register the snippet with Transact-SQLSQL Server Management Studio. 您可以加入包含多個程式碼片段的資料夾,或是將個別程式碼片段匯入到 [My Code 程式碼片段] 資料夾。You can either add a folder containing multiple snippets, or import individual snippets to the My Code Snippets folder.

程序Procedures

加入程式碼片段資料夾Adding a Snippet Folder

  1. 開啟 Transact-SQLSQL Server Management StudioOpen Transact-SQLSQL Server Management Studio.

  2. 選取 [工具] 功能表,然後按一下 [程式碼片段管理員]Select the Tools menu, and click Code Snippets Manager.

  3. 按一下 [加入] 按鈕。Click the Add button.

  4. 導覽至包含程式碼片段的資料夾,然後按一下 [選取資料夾] 按鈕。Navigate to the folder containing your code snippets, and click the Select Folder button.

匯入程式碼片段Importing a Snippet

  1. 開啟 Transact-SQLSQL Server Management StudioOpen Transact-SQLSQL Server Management Studio.

  2. 選取 [工具] 功能表,然後按一下 [程式碼片段管理員]Select the Tools menu, and click Code Snippets Manager.

  3. 按一下 [匯入] 按鈕。Click the Import button.

  4. 導覽至包含程式碼片段的資料夾,然後按一下 .snippet 檔案,再按一下 [開啟] 按鈕。Navigate to the folder containing your snippet, click on the .snippet file, and click the Open button.

範例Examples

下列範例會建立 TRY-CATCH 範圍陳述式程式碼片段,並將其匯入至 Transact-SQLSQL Server Management StudioThe following example creates a TRY-CATCH surround-with snippet and imports it to Transact-SQLSQL Server Management Studio.

  1. 將下列程式碼貼到 [記事本],然後將檔案儲存為 TryCatch.snippet 名稱。Paste the following code into notepad, then save as a file named TryCatch.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>  
       <_locTag _loc="locData">Default</_locTag>  
    </_locDefinition>  
    <CodeSnippet Format="1.0.0">  
    <Header>  
    <Title>TryCatch</Title>  
                            <Shortcut></Shortcut>  
    <Description>Example Snippet for Try-Catch.</Description>  
    <Author>SQL Server Books Online Example</Author>  
    <SnippetTypes>  
                                    <SnippetType>SurroundsWith</SnippetType>  
    </SnippetTypes>  
    </Header>  
    <Snippet>  
    <Declarations>  
                                    <Literal>  
                                    <ID>CatchCode</ID>  
                                    <ToolTip>Code to handle the caught error</ToolTip>  
                                    <Default>CatchCode</Default>  
                                    </Literal>  
    </Declarations>  
    <Code Language="SQL"><![CDATA[  
    BEGIN TRY  
    
    $selected$ $end$  
    
    END TRY  
    BEGIN CATCH  
    
    $CatchCode$  
    
    END CATCH;  
    ]]>  
    </Code>  
    </Snippet>  
    </CodeSnippet>  
    </CodeSnippets>  
    
  2. 開啟 Transact-SQLSQL Server Management StudioOpen Transact-SQLSQL Server Management Studio.

  3. 選取 [工具] 功能表,然後按一下 [程式碼片段管理員]Select the Tools menu, and click Code Snippets Manager.

  4. 按一下 [匯入] 按鈕。Click the Import button.

  5. 導覽至包含 TryCatch.snippet 的資料夾,然後按一下 TryCatch.snippet 檔案,再按一下 [開啟] 按鈕。Navigate to the folder containing TryCatch.snippet, click on the TryCatch.snippet file, and click the Open button. [My Code 程式碼片段] 資料夾中應該不會有 TryCatch 程式碼片段。You should not have a TryCatch snippet in your My Code Snippets folder.

另請參閱See Also

插入範圍陳述式 Transact-SQL 程式碼片段Insert Surround-with Transact-SQL snippets