Snippets are a great productivity feature to speed up typing of repetitive T-SQL. Snippets were introduced in SQL Server 2012 as part of SQL Server Management Studio (SSMS).
代码片段是一项强大的生产力功能,可加快重复性T-SQL的键入速度。 作为SQL Server Management Studio(SSMS)的一部分,SQL Server 2012中引入了代码片段。
Snippets are XML templates with predefined fields and values that render into T-SQL, when processed by the query window.
代码段是具有预定义字段和值的XML模板,当查询窗口对其进行处理时,这些字段和值会呈现为T-SQL。
In the example below is the results of the Create Trigger snippet that inserts T-SQL required to create a trigger in the query window at the current cursor position. The highlighted elements are placeholders that are replaced with values that the user inputs:
在下面的示例中是创建触发器代码段的结果,该代码段插入了在查询窗口中当前光标位置创建触发器所需的T-SQL。 高亮显示的元素是占位符,已被用户输入的值替换:
There are three categories of SQL snippets:
SQL片段分为三类:
Default snippets
These SQL snippets come, out-of-the-box with SSMS and they are placed under folders based on category (e.g. Table, Function, Index).默认片段
这些SQL代码段随SSMS一起提供,并且根据类别(例如,表,函数,索引)放置在文件夹下。Surround snippets
These three snippets, found in the Function folder, help to implement code structure like Begin End, While, and If around selected text in the query window.环绕片段
位于Function文件夹中的这三个代码段有助于实现诸如Begin End , While , 如果在查询窗口中选中的文本周围。Custom snippets
Users can create their own custom snippets. Here is an example of the “plumbing” underneath a custom SQL snippet.自定义片段
用户可以创建自己的自定义片段。 这是自定义SQL代码段下的“管道”示例。…and here how it looks when is initiated in the query window.
…以及在查询窗口中启动时的外观。
By default snippets for SSMS 2016 are found under this path:
默认情况下,在以下路径中找到SSMS 2016的代码段:
C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio\SQL\Snippets\1033\
C:\ Program Files(x86)\ Microsoft SQL Server \ 130 \ Tools \ Binn \ ManagementStudio \ SQL \ Snippets \ 1033 \
The location of SQL snippets depends on the version of the SSMS that is used. So if SSMS 2014 is used, the default path on which there snippets are: C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\SQL\Snippets\1033\
SQL代码段的位置取决于所使用的SSMS的版本。 因此,如果使用SSMS 2014,则其片段所在的默认路径为:C:\ Program Files(x86)\ Microsoft SQL Server \ 120 \ Tools \ Binn \ ManagementStudio \ SQL \ Snippets \ 1033 \
The only difference is in the name of the folder that come after the Microsoft SQL Server folder (e.g. folder 130 for SSMS 2016, 120 for SSMS 2014.)
唯一的区别在于Microsoft SQL Server文件夹之后的文件夹名称(例如,SSMS 2016的文件夹130,SSMS 2014的文件夹120)。
To learn more about these folders please visit this link.
要了解有关这些文件夹的更多信息,请访问此链接 。
To save a SQL snippet the .snippet extension must be used
要保存SQL代码段,必须使用.snippet扩展名
插入内置代码段 (Inserting a built-in snippet )
There are three ways to insert a built-in SQL snippet that comes with SSMS in the query window:
有三种方法可以在查询窗口中插入SSMS随附的内置SQL代码段:
In the SSMS main menu under the Edit menu, choose the IntelliSense option and select the Insert Snippet command:
在“ 编辑”菜单下的“ SSMS”主菜单中,选择“ 智能感知”选项,然后选择“ 插入代码段”命令:
Right-click in the query window will show popup menu and from the list choose the Insert Snippet command:
在查询窗口中单击鼠标右键将显示弹出菜单,然后从列表中选择“ 插入代码段”命令:
Using the combination of keyboard shortcut Ctrl+K followed by Ctrl+X
结合使用键盘快捷键Ctrl + K和Ctrl + X
Select a category e.g. Function to see all available snippets under that category:
选择一个类别,例如“功能”以查看该类别下的所有可用片段:
Double click on the desired snippet from the list will insert code snippet in the query window with highlighted placeholders that need to be modified: