ssms排序代码

This article explores the SSMS Code Snippet feature and ways to customize it.

本文探讨了SSMS代码段功能及其自定义方法。

介绍 (Introduction)

SSMS is a popular client tool for SQL Server to do both administrative and development tasks. Microsoft has excluded the SQL Server management studio from the SQL Server setup. It is a standalone client program with frequent updates and bug-fixes.

SSMS是SQL Server可以执行管理和开发任务的流行客户端工具。 Microsoft已从SQL Server安装程序中排除了SQL Server管理工作室。 它是一个独立的客户端程序,具有频繁的更新和错误修复。

  • Current SSMS release: 18.4 (release date: November 04, 2019) 当前的SSMS版本 :18.4(发布日期:2019年11月4日)

In the article SQL Code Snippets in Azure Data Studio, we learned using the code snippets feature in cross-platform Azure Data Studio. Do we have the same functionality with SSMS? Let’s explore in this article.

在文章《 Azure Data Studio中SQL代码片段》中 ,我们学习了如何在跨平台Azure Data Studio中使用代码片段功能。 SSMS是否具有相同的功能? 让我们在本文中进行探索。

Connect to SQL Server instance and open a new query window. Right-click on the query editor, and it gives you a list of options, as shown below.

连接到SQL Server实例并打开一个新的查询窗口。 右键单击查询编辑器,它会为您提供选项列表,如下所示。

Have you explored the highlighted options- Insert Snippet and Surround with? If not, follow the article to understand it.

您是否浏览过突出显示的选项- 插入摘录环绕声? 如果不是,请按照文章进行理解。

插入代码段 (Insert Code Snippets)

We use code snippets for quickly writing queries without any syntax errors. It is like a code template that you can customize as per your requirement.

我们使用代码片段快速编写查询,而不会出现任何语法错误。 它就像一个代码模板,您可以根据需要自定义。

Click on the Insert Snippets in a new query window ( as per the above screenshot). Alternatively, press CTRL+K. You get the following message CTRL+K was pressed. Waiting for the second key of the chord below the status bar.

单击“在新查询窗口中插入片段”(如上述屏幕截图所示)。 或者,按CTRL + K。 您收到以下消息,按了CTRL + K。 等待状态栏下方的和弦第二个键

Now, press CTRL+X, and it gives you a snippets category as per the object group.

现在,按CTRL + X,它会根据对象组为您提供一个摘要类别。

These snippet categories are as follows.

这些代码片段类别如下。

  • Function 功能
  • Index 指数
  • Login 登录
  • Role 角色
  • Schema 架构图
  • Stored procedure 存储过程
  • Synonym 代名词
  • Table 表
  • Trigger 触发
  • User 用户
  • User-defined data type 用户定义的数据类型
  • User-defined table type 用户定义表类型
  • User-defined 用户自定义
  • View 视图

Suppose we want to create a SQL table for our demonstration. Navigate to the table and press enter. It gives available code snippets for the table category. You can see below that it contains a single snippet – Create a table for this category.

假设我们要为我们的演示创建一个SQL表。 导航到表格,然后按Enter。 它提供了表类别的可用代码段。 您可以在下面看到它包含一个片段–为该类别创建一个表。

Click on the create table, and it populates create table definition for you.

单击创建表,它会为您填充创建表定义。

It also highlights the code that you can change for creating a table. For example, we can change the table name, column name, and data types.

它还突出显示了您可以更改的用于创建表的代码。 例如,我们可以更改表名,列名和数据类型。

Similarly, for the index category, we get three code snippets. You can navigate to the individual snippet, and it gives a brief introduction as well.

同样,对于索引类别,我们获得了三个代码段。 您可以导航到各个代码段,并且也进行了简要介绍。

  • Create Index basic 基本创建索引
  • Create primary XML index 创建主要的XML索引
  • Create Unique Non-Clustered Indexes 创建唯一的非聚集索引

Select Create Index basic and press Enter. It gives the following code for it.

选择基本创建索引,然后按Enter。 它提供了以下代码。

If we choose the stored procedure, we get the following templates.

如果选择存储过程,则会得到以下模板。

  • Create procedure basic template 创建过程基本模板
  • Create procedure with CURSOR OUTPUT parameter 使用CURSOR OUTPUT参数创建过程
  • Create procedure with OUTPUT parameter 使用OUTPUT参数创建过程

环绕功能 (Surround with feature )

In the code, we use begin, while, and If code blocks for defining various logics. We can use surround with a feature for inserting templates of Begin, While and If logic.

在代码中,我们使用beginwhileIf代码块来定义各种逻辑。 我们可以使用带有环绕声的功能来插入Begin,While和If逻辑模板。

You can either click on the surround within SSMS options( refer to the first screenshot). Alternatively, press CTRL+K followed by CTRL+S. You can see templates for Begin, If and While.

您可以在SSMS选项中单击环绕(请参阅第一个屏幕截图)。 或者,按CTRL + K,然后按CTRL + S。 您可以看到Begin,If和While的模板。

We can use these in combination with the code snippets explained above.

我们可以将它们与上述代码段结合使用。

For example, first, open a stored procedure basic template.

例如,首先,打开一个存储过程基本模板。

Make the required changes, such as procedure name and parameters. We can see below the modified code.

进行所需的更改,例如过程名称和参数。 我们可以在下面看到修改后的代码。

Now, suppose we want a while loop in the stored procedure. This while loop should surround the select statement of the procedure.

现在,假设我们要在存储过程中进行while循环。 该while循环应围绕过程的select语句。

Highlight the select statement that we want to surround with a while loop.

用while循环突出显示我们要包围的select语句。

Now, press CTRL+K followed by CTRL+S, and chose while loop from the drop-down.

现在,按CTRL + K,再按CTRL + S,然后从下拉菜单中选择while循环。

Press Enter and while loop surrounds the highlighted select statement. It also highlights condition word that specifies you require configuration of condition for a while loop.

按Enter键,而循环围绕突出显示的选择语句。 它还突出显示条件字,该条件字指定您需要配置while循环的条件。

Specify condition logic in the while loop of the stored procedure, and you are good to implement this in your instance.

在存储过程的while循环中指定条件逻辑,您可以在实例中很好地实现此条件。

Similarly, you can use If and Begin loops using surround with option.

同样,您可以使用带环绕声选项的If和Begin循环。

代码片段管理器 (Code Snippets Manager)

We have code snippets manager to manage the available code snippets. In the menu bar, navigate to Tools and click on Code Snippets Manager.

我们有代码片段管理器来管理可用的代码片段。 在菜单栏中,导航到“工具”,然后单击“代码片段管理器”。

Alternatively, press CTRL+K followed by CTRL+B, to launch it.

或者,按CTRL + K,然后按CTRL + B,以启动它。

In this code snippets manager, we a view, add or remove available code snippets. You can also create a new SSMS code snippet and import it under the respective group.

在此代码段管理器中,我们查看,添加或删除可用的代码段。 您还可以创建一个新的SSMS代码段,并将其导入相应的组中。

Click on required code snippet, and it shows brief information about snippet.

单击所需的代码片段,它显示有关该片段的简要信息。

You can also view the location of code snippets in the Microsoft SQL Server Management Studio folder. For SSMS 18, it stores all snippets in C:\Program Files (x86)\Microsoft SQL Server Management Studio 18\Common7\IDE\SQL\Snippets\1033\Table\Create Table.snippet.

您还可以在Microsoft SQL Server Management Studio文件夹中查看代码段的位置。 对于SSMS 18,它将所有代码段存储在C:\ Program Files(x86)\ Microsoft SQL Server Management Studio 18 \ Common7 \ IDE \ SQL \ Snippets \ 1033 \ Table \ Create Table.snippet中。

Suppose we want to customize this create table snippet. To make the changes, go to the snippet directory and create a copy of the required snippet. We can directly make changes as well, but it is always better to make changes in a copy and later import with a different name.

假设我们要自定义此创建表代码段。 要进行更改,请转到代码段目录并创建所需代码段的副本。 我们也可以直接进行更改,但是最好在副本中进行更改,然后再使用其他名称进行导入。

Open this snippet in Visual Studio or an appropriate XML editor.

在Visual Studio或适当的XML编辑器中打开此代码段。

Let’s make the following changes in this XML code.

让我们在此XML代码中进行以下更改。

  • Modify title, description, description, and author of the new code snippet

    修改新代码段的标题,描述,描述和作者

  • Change the schema from dbo to Person and modify the data type of column 1 from int to varchar(50)

    将模式从dbo更改为Person,并将列1的数据类型从int更改为varchar(50)

  • Add a new column. Specify the name of a column – Column_3 and data type bit. We can copy-paste the previous column details and rename the column properties

    添加一个新列。 指定列的名称– Column_3和数据类型位。 我们可以复制粘贴上一列的详细信息,然后重命名该列的属性

  • Specify the column in create table command, as shown below

    在create table命令中指定列,如下所示

  • Save the new code snippet and give it an appropriate name

    保存新的代码段并为其指定适当的名称

Now, let’s check this code snippet in a new query window. We see this custom snippet, as shown below. You can see description Create a table- Demo Rajendra for this as specified in XML.

现在,让我们在新的查询窗口中检查此代码段。 我们看到了这个自定义代码段,如下所示。 您可以看到描述创建表-为此, 演示了XML中指定的Rajendra

Press Enter and view the code for this custom snippet. It satisfies our requirements specified above.

按Enter键,然后查看此自定义代码段的代码。 满足我们上面指定的要求。

为sp_helpdb添加自定义代码段 (Add a custom code snippet for sp_helpdb )

Let’s create a basic code snippet that executes sp_helpdb commands for SQL Server. You can reference XML of existing code snippets. Here, defines the title, description, author, snippet type, and code in XML format. Below is the reference XML for the sp_helpdb system procedure.

让我们创建一个基本代码片段,该片段为SQL Server执行sp_helpdb命令。 您可以引用现有代码段的XML。 在此,以XML格式定义标题,描述,作者,代码段类型和代码。 下面是sp_helpdb系统过程的参考XML。

<?XML version="1.0" encoding="UTF-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"><CodeSnippet Format="1.0.0"><Header><Title>sp_helpdb</Title><Shortcut /><Description>It returns output of sp_helpdb</Description><Author>Rajendra Gupta</Author><SnippetTypes><SnippetType>Expansion</SnippetType></SnippetTypes></Header><Snippet><Code Language="SQL"><![CDATA[Exec sp_helpdb]]></Code></Snippet></CodeSnippet>
</CodeSnippets>

Launch code snippet manager from SSMS toolbox and click on Import. Browse to a directory that contains a code snippet file.

从SSMS工具箱中启动代码段管理器,然后单击“导入”。 浏览到包含代码段文件的目录。

It validates the code definition and saves it in the “my code snippets folder”.

它将验证代码定义并将其保存在“我的代码片段文件夹”中。

Once the import is done, we can view its property as shown below.

导入完成后,我们可以查看其属性,如下所示。

Now, view the code snippet in my code snippet folder and press Enter on it.

现在,查看我的代码片段文件夹中的代码片段,然后按Enter。

It prints the query specified in the code snippet.

它打印代码段中指定的查询。

结论 (Conclusion)

In this article, we explored the useful code snippets feature of SSMS. It helps you write queries quickly, effectively. It is useful for beginner also those are not familiar with t-SQL syntax. We can also customize them as per your requirements.

在本文中,我们探讨了SSMS的有用的代码片段功能。 它可以帮助您快速,有效地编写查询。 对于不熟悉t-SQL语法的初学者也很有用。 我们也可以根据您的要求自定义它们。

翻译自: https://www.sqlshack.com/an-overview-of-code-snippets-in-ssms/

ssms排序代码

ssms排序代码_SSMS中的代码片段概述相关推荐

  1. 云效Codeup代码评审中的代码协同

    简介: 云效 Codeup 汇集了阿里巴巴最新的代码托管.代码协同技术,希望能够造福更多中国和世界的开发者. 大神说:"Show me the code",于是就有了代码评审. & ...

  2. 代码评审中的代码协同

    简介: 代码评审中同样存在着"Talk is cheap. Show me the code",语言无力时,直接上代码吧.这就是我们今天要讨论的话题--代码评审中的代码协同. 作者 ...

  3. java常用代码_Java 中常用代码 (欢迎补充)

    //-------------------------------------------------------------------------------------------------- ...

  4. Java 中静态代码块 static的作用及用法

    java静态代码块的作用: Java静态代码块中的代码会在类加载JVM时运行,且只被执行一次,也就是说这些代码不需要实例化类就能够被调用.一般情况下,如果有些代码必须在项目启动的时候就执行的时候,就需 ...

  5. 在word中插入代码段的方法[转]

    废话不多说,下面说明实现步骤 步骤一 1.打开这个代码高亮工具网站:http://www.planetb.ca/syntax-highlight-word 2.在代码框中粘贴代码,选择语言,点击Sho ...

  6. 如何向Word中插入代码块

    如何向Word中插入代码块 对于一些用Word写的论文,作业中如果要插入代码,无论是直接复制粘贴还是加上灰色无边框的效果看上去十分不美观, 优雅的插入代码块给别人的印象就会好很多. 思路:将代码块的原 ...

  7. 让服务器自动从HG版本库中下载代码

    让服务器自动从HG版本库中下载代码 每次写完代码,提交到版本库,测试可以执行不会冲突,推到远端代码仓库.之后要发布的话,还要通过FTP上传到服务器上,FTP速度又不很理想,严重影响工作效率. 有没有解 ...

  8. java fianlly_Java冷知识:finally中的代码一定会执行吗?

    前言 Java开发中,正常情况下,在执行了try代码块之后,finally中的代码一定会执行.我们实际开发也经常会利用这个特性,在finally中来执行一些特殊的操作,比如:释放资源.释放锁等. de ...

  9. 团队管理中的代码评审

    代码评审在软件项目管理中是经常组织的活动,通过代码评审的工作也确实给我们的团队带来很多的益处,简单谈谈代码评审的感受,你们的团队是否也在进行代码评审(Code Review)的相关工作呢? 1.为什么 ...

最新文章

  1. Google Test(GTest)使用方法和源码解析——Listener技术分析和应用
  2. OpenCV与Python之图像阈值化
  3. SQL Server 堆heap 非聚集索引 Nonclustered index 行号键查找RID loopup结合执行计划过程详解
  4. C语言九十四之请编写函数fun(char *str, int n),其功能是:使字符串str的前导*号不能多余n个,若多于n个,则删除多余的*号,若少于或等于n个,则不做处理。
  5. 2021牛客暑期多校训练营3 I-Kuriyama Mirai and Exclusive Or(异或+差分)
  6. docker集群_使用Docker,Chef和Amazon OpsWorks进行集群范围的Java / Scala应用程序部署...
  7. Netty之Bootstrap详解
  8. php cgi fastcgi php-fpm区别
  9. luogu P2680 运输计划 (二分答案+树上差分)
  10. System.Data.SQLite兼容32位和64位问题
  11. STC系列单片机烧录
  12. Mac下 CMake 安装及环境配置
  13. 超宽带 DWM1000模块 校准
  14. python面向对象编程指南pdf_Python面向对象编程指南(pdf+epub+mobi+txt+azw3)
  15. 使用PS改变背景为透明
  16. [转载] 过 DNF TP 驱动保护
  17. 【转载】GitHub中国区前100名到底是什么样的人
  18. 完美时空客服自助平台SQL注射漏洞
  19. ::before和::after伪元素的使用及优惠券案例
  20. Nginx同一个域名配置多个项目

热门文章

  1. go interface类型转换_Go语言的九大核心特性主要有哪些?
  2. java enum转ini_JAVA中用XML实现INI文件格式的解决方
  3. yosemite php gd,Yosemite下配置PHP支持GD库FreeType
  4. 想念你 2012-10-24 23:27
  5. 【android】Airtest IDE实现多设备管理以及自动装包
  6. JPA学习笔记二——Hello World
  7. 【动态规划】天上掉馅饼
  8. word2vec C源码解析
  9. 怎样在virtualbox下WIN7共享XP的文件
  10. 计算机网络学习笔记(13. OSI参考模型①)