|
1 | | ---- |
2 | | -title: "Add Transact-SQL Snippets | Microsoft Docs" |
3 | | -ms.custom: "" |
4 | | -ms.date: "03/14/2017" |
5 | | -ms.prod: sql |
6 | | -ms.prod_service: "sql-tools" |
7 | | -ms.technology: scripting |
8 | | -ms.reviewer: "" |
9 | | -ms.topic: conceptual |
10 | | -ms.assetid: 901c7995-8eb5-4d12-8bb0-de0a922b48f8 |
11 | | -author: MightyPen |
12 | | -ms.author: genemi |
13 | | -manager: craigg |
14 | | -monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||=sqlallproducts-allversions||>=sql-server-linux-2017||=azuresqldb-mi-current" |
15 | | ---- |
16 | | -# Add Transact-SQL Snippets |
17 | | -[!INCLUDE[appliesto-ss-asdb-asdw-pdw-md](../../includes/appliesto-ss-asdb-asdw-pdw-md.md)] |
18 | | - You can add your own Transact-SQL code snippets to the set of pre-defined snippets included in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]. |
19 | | - |
20 | | -## Creating a Transact-SQL Snippet File |
21 | | - The first part of creating a [!INCLUDE[tsql](../../includes/tsql-md.md)] code snippet is to create an XML file with the text of your code snippet. The file must have a .snippet file extension, and meet the requirements of the [Code Snippets Schema](http://go.microsoft.com/fwlink/?LinkId=207504). Set the snippet language to SQL. |
22 | | - |
23 | | - You can use the pre-defined snippets that ship with [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] as examples. To find the pre-defined snippets, open [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)], select the **Tools** menu, and click **Code Snippet Manager**. Select **SQL** in the **Language** list box, the path to the [!INCLUDE[tsql](../../includes/tsql-md.md)] snippets is displayed in the **Location** box. |
24 | | - |
25 | | -## Registering the Code Snippet |
26 | | - After creating the snippet file, use the Code Snippets Manager to register the snippet with [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. You can either add a folder containing multiple snippets, or import individual snippets to the **My Code Snippets** folder. |
27 | | - |
28 | | -## Procedures |
29 | | - |
30 | | -#### Adding a Snippet Folder |
31 | | - |
32 | | -1. Open [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. |
33 | | - |
34 | | -2. Select the **Tools** menu, and click **Code Snippets Manager**. |
35 | | - |
36 | | -3. Click the **Add** button. |
37 | | - |
38 | | -4. Navigate to the folder containing your code snippets, and click the **Select Folder** button. |
39 | | - |
40 | | -#### Importing a Snippet |
41 | | - |
42 | | -1. Open [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. |
43 | | - |
44 | | -2. Select the **Tools** menu, and click **Code Snippets Manager**. |
45 | | - |
46 | | -3. Click the **Import** button. |
47 | | - |
48 | | -4. Navigate to the folder containing your snippet, click on the .snippet file, and click the **Open** button. |
49 | | - |
50 | | -## Examples |
51 | | - The following example creates a **TRY-CATCH** surround-with snippet and imports it to [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. |
52 | | - |
53 | | -1. Paste the following code into notepad, then save as a file named TryCatch.snippet. |
54 | | - |
55 | | - ``` |
56 | | - <?xml version="1.0" encoding="utf-8" ?> |
57 | | - <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> |
58 | | - <_locDefinition xmlns="urn:locstudio"> |
59 | | - <_locDefault _loc="locNone" /> |
60 | | - <_locTag _loc="locData">Title</_locTag> |
61 | | - <_locTag _loc="locData">Description</_locTag> |
62 | | - <_locTag _loc="locData">Author</_locTag> |
63 | | - <_locTag _loc="locData">ToolTip</_locTag> |
64 | | - <_locTag _loc="locData">Default</_locTag> |
65 | | - </_locDefinition> |
66 | | - <CodeSnippet Format="1.0.0"> |
67 | | - <Header> |
68 | | - <Title>TryCatch</Title> |
69 | | - <Shortcut></Shortcut> |
70 | | - <Description>Example Snippet for Try-Catch.</Description> |
71 | | - <Author>SQL Server Books Online Example</Author> |
72 | | - <SnippetTypes> |
73 | | - <SnippetType>SurroundsWith</SnippetType> |
74 | | - </SnippetTypes> |
75 | | - </Header> |
76 | | - <Snippet> |
77 | | - <Declarations> |
78 | | - <Literal> |
79 | | - <ID>CatchCode</ID> |
80 | | - <ToolTip>Code to handle the caught error</ToolTip> |
81 | | - <Default>CatchCode</Default> |
82 | | - </Literal> |
83 | | - </Declarations> |
84 | | - <Code Language="SQL"><![CDATA[ |
85 | | - BEGIN TRY |
86 | | - |
87 | | - $selected$ $end$ |
88 | | - |
89 | | - END TRY |
90 | | - BEGIN CATCH |
91 | | - |
92 | | - $CatchCode$ |
93 | | - |
94 | | - END CATCH; |
95 | | - ]]> |
96 | | - </Code> |
97 | | - </Snippet> |
98 | | - </CodeSnippet> |
99 | | - </CodeSnippets> |
100 | | - ``` |
101 | | - |
102 | | -2. Open [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. |
103 | | - |
104 | | -3. Select the **Tools** menu, and click **Code Snippets Manager**. |
105 | | - |
106 | | -4. Click the **Import** button. |
107 | | - |
108 | | -5. Navigate to the folder containing TryCatch.snippet, click on the TryCatch.snippet file, and click the **Open** button. You should not have a TryCatch snippet in your **My Code Snippets** folder. |
109 | | - |
110 | | -## See Also |
111 | | - [Insert Surround-with Transact-SQL snippets](../../relational-databases/scripting/insert-surround-with-transact-sql-snippets.md) |
112 | | - |
113 | | - |
| 1 | +--- |
| 2 | +title: "Add Transact-SQL Snippets | Microsoft Docs" |
| 3 | +ms.custom: "" |
| 4 | +ms.date: "03/14/2017" |
| 5 | +ms.prod: sql |
| 6 | +ms.prod_service: "sql-tools" |
| 7 | +ms.technology: scripting |
| 8 | +ms.reviewer: "" |
| 9 | +ms.topic: conceptual |
| 10 | +ms.assetid: 901c7995-8eb5-4d12-8bb0-de0a922b48f8 |
| 11 | +author: MightyPen |
| 12 | +ms.author: genemi |
| 13 | +manager: craigg |
| 14 | +monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||=sqlallproducts-allversions||>=sql-server-linux-2017||=azuresqldb-mi-current" |
| 15 | +--- |
| 16 | +# Add Transact-SQL Snippets |
| 17 | +[!INCLUDE[appliesto-ss-asdb-asdw-pdw-md](../../includes/appliesto-ss-asdb-asdw-pdw-md.md)] |
| 18 | + You can add your own Transact-SQL code snippets to the set of pre-defined snippets included in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]. |
| 19 | + |
| 20 | +## Creating a Transact-SQL Snippet File |
| 21 | + The first part of creating a [!INCLUDE[tsql](../../includes/tsql-md.md)] code snippet is to create an XML file with the text of your code snippet. The file must have a .snippet file extension, and meet the requirements of the [Code Snippets Schema](http://go.microsoft.com/fwlink/?LinkId=207504). Set the snippet language to SQL. |
| 22 | + |
| 23 | + You can use the pre-defined snippets that ship with [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] as examples. To find the pre-defined snippets, open [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)], select the **Tools** menu, and click **Code Snippet Manager**. Select **SQL** in the **Language** list box, the path to the [!INCLUDE[tsql](../../includes/tsql-md.md)] snippets is displayed in the **Location** box. |
| 24 | + |
| 25 | +## Registering the Code Snippet |
| 26 | + After creating the snippet file, use the Code Snippets Manager to register the snippet with [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. You can either add a folder containing multiple snippets, or import individual snippets to the **My Code Snippets** folder. |
| 27 | + |
| 28 | +## Procedures |
| 29 | + |
| 30 | +#### Adding a Snippet Folder |
| 31 | + |
| 32 | +1. Open [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. |
| 33 | + |
| 34 | +2. Select the **Tools** menu, and click **Code Snippets Manager**. |
| 35 | + |
| 36 | +3. Click the **Add** button. |
| 37 | + |
| 38 | +4. Navigate to the folder containing your code snippets, and click the **Select Folder** button. |
| 39 | + |
| 40 | +#### Importing a Snippet |
| 41 | + |
| 42 | +1. Open [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. |
| 43 | + |
| 44 | +2. Select the **Tools** menu, and click **Code Snippets Manager**. |
| 45 | + |
| 46 | +3. Click the **Import** button. |
| 47 | + |
| 48 | +4. Navigate to the folder containing your snippet, click on the .snippet file, and click the **Open** button. |
| 49 | + |
| 50 | +## Examples |
| 51 | + The following example creates a **TRY-CATCH** surround-with snippet and imports it to [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. |
| 52 | + |
| 53 | +1. Paste the following code into notepad, then save as a file named TryCatch.snippet. |
| 54 | + |
| 55 | + ``` |
| 56 | + <?xml version="1.0" encoding="utf-8" ?> |
| 57 | + <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> |
| 58 | + <_locDefinition xmlns="urn:locstudio"> |
| 59 | + <_locDefault _loc="locNone" /> |
| 60 | + <_locTag _loc="locData">Title</_locTag> |
| 61 | + <_locTag _loc="locData">Description</_locTag> |
| 62 | + <_locTag _loc="locData">Author</_locTag> |
| 63 | + <_locTag _loc="locData">ToolTip</_locTag> |
| 64 | + <_locTag _loc="locData">Default</_locTag> |
| 65 | + </_locDefinition> |
| 66 | + <CodeSnippet Format="1.0.0"> |
| 67 | + <Header> |
| 68 | + <Title>TryCatch</Title> |
| 69 | + <Shortcut></Shortcut> |
| 70 | + <Description>Example Snippet for Try-Catch.</Description> |
| 71 | + <Author>SQL Server Books Online Example</Author> |
| 72 | + <SnippetTypes> |
| 73 | + <SnippetType>SurroundsWith</SnippetType> |
| 74 | + </SnippetTypes> |
| 75 | + </Header> |
| 76 | + <Snippet> |
| 77 | + <Declarations> |
| 78 | + <Literal> |
| 79 | + <ID>CatchCode</ID> |
| 80 | + <ToolTip>Code to handle the caught error</ToolTip> |
| 81 | + <Default>CatchCode</Default> |
| 82 | + </Literal> |
| 83 | + </Declarations> |
| 84 | + <Code Language="SQL"><![CDATA[ |
| 85 | + BEGIN TRY |
| 86 | + |
| 87 | + $selected$ $end$ |
| 88 | + |
| 89 | + END TRY |
| 90 | + BEGIN CATCH |
| 91 | + |
| 92 | + $CatchCode$ |
| 93 | + |
| 94 | + END CATCH; |
| 95 | + ]]> |
| 96 | + </Code> |
| 97 | + </Snippet> |
| 98 | + </CodeSnippet> |
| 99 | + </CodeSnippets> |
| 100 | + ``` |
| 101 | + |
| 102 | +2. Open [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. |
| 103 | + |
| 104 | +3. Select the **Tools** menu, and click **Code Snippets Manager**. |
| 105 | + |
| 106 | +4. Click the **Import** button. |
| 107 | + |
| 108 | +5. Navigate to the folder containing TryCatch.snippet, click on the TryCatch.snippet file, and click the **Open** button. You should not have a TryCatch snippet in your **My Code Snippets** folder. |
| 109 | + |
| 110 | +## See Also |
| 111 | + [Insert Surround-with Transact-SQL snippets](../../relational-databases/scripting/insert-surround-with-transact-sql-snippets.md) |
| 112 | + |
| 113 | + |
0 commit comments