SQL Server Management Studio中SQL代码段

本文介绍了SQL Server Management Studio (SSMS) 中的代码段功能,包括默认代码段、环绕代码段和自定义代码段的创建与使用方法。代码段是一种提高SQL开发效率的工具,可以通过XML模板快速生成常见的T-SQL代码。文章详细阐述了如何注册、导入代码段,以及如何在查询窗口中使用它们。
摘要由CSDN通过智能技术生成

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片段分为三类:

  1. 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一起提供,并且根据类别(例如,表,函数,索引)放置在文件夹下。

  2. 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 EndWhile 如果在查询窗口中选中的文本周围。

  3. 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.

    …以及在查询窗口中启动时的外观。

  4. 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代码段:

    1. In the SSMS main menu under the Edit menu, choose the IntelliSense option and select the Insert Snippet command:

      在“ 编辑”菜单下的“ SSMS”主菜单中,选择“ 智能感知”选项,然后选择“ 插入代码段”命令:

    2. Right-click in the query window will show popup menu and from the list choose the Insert Snippet command:

      在查询窗口中单击鼠标右键将显示弹出菜单,然后从列表中选择“ 插入代码段”命令:

    3. 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:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值