SQL Server Management Studio(SSMS)中的脚本模板

SQL Server Management Studio (SSMS) is a popular client tool to connect with SQL Server and perform various development and administrative tasks. Beginner developers or DBAs might not be familiar with T-SQL or code for specific tasks. Even for an expert DBA, query templates and shortcuts help immensely to save time and improve productivity. SSMS template explorer is an often-overlooked solution, but it is a useful feature.

SQL Server Management Studio(SSMS)是一种流行的客户端工具,可与SQL Server连接并执行各种开发和管理任务。 初学者或DBA可能不熟悉T-SQL或特定任务的代码。 即使对于专业的DBA,查询模板和快捷方式也可以极大地帮助您节省时间并提高生产率。 SSMS模板资源管理器是一个经常被忽视的解决方案,但是它是一个有用的功能。

SSMS template explorer is a collection of useful query templates. You need to provide the required inputs and execute the query. For example, in a create database template, we only need to specify the new database name.

SSMS模板资源管理器是有用的查询模板的集合。 您需要提供所需的输入并执行查询。 例如,在创建数据库模板中,我们仅需要指定新的数据库名称。

先决条件 (Prerequisite)

In this article, I am using the latest SSMS 18.5 version released on April 07, 2020. You can download it from Microsoft Docs. You can use previous versions of SSMS as well for this article; however, I would recommend using the latest version.

在本文中,我使用的是2020年4月7日发布的最新SSMS 18.5版本。您可以从Microsoft Docs下载它。 您也可以在本文中使用SSMS的早期版本。 但是,我建议使用最新版本。

SSMS latest version

SSMS中的模板资源管理器
(Template Explorer in SSMS
)

We can launch Template Explorer from the SSMS menu bar. Navigate to View-> Template Explorer.

Template Explorer

我们可以从SSMS菜单栏中启动Template Explorer。 导航到“ 视图”->“模板资源管理器”。

Alternatively, we can also use a keyboard shortcut Ctrl + Alt + T for this template explorer. It opens the following template browser.

另外,我们也可以使用键盘快捷键Ctrl + Alt + T来访问此模板浏览器。 它打开以下模板浏览器。

It groups the templates as per their categories in the various folders. It provides templates for both database and analysis services.

它将模板按类别分类在各个文件夹中。 它提供了数据库和分析服务的模板。

In the bottom section, you can see recently used templates. For example, I opened the Create Database template recently, so it shows up here. In this way, you can easily pick the recently used template without going through folders.

在底部,您可以看到最近使用的模板。 例如,我最近打开了“ 创建数据库”模板,因此它显示在这里。 这样,您可以轻松选择最近使用的模板,而无需浏览文件夹。

Template Browser

Let’s expand the database folder and view the templates inside it.

让我们展开数据库文件夹并查看其中的模板。

View templates

It shows scripts to create the database, create a database with multiple filegroups, create a database with the filstream filegroup, bring the database online, take the database offline, attach and detach the database. These are the everyday tasks we do in a database.

它显示了用于创建数据库,创建具有多个文件组的数据库,使用filstream文件组创建数据库,使数据库联机,使数据库脱机,附加和分离数据库的脚本。 这些是我们在数据库中执行的日常任务。

Let’s say I want to create a database without specifying any additional filegroup. Now, double-click on the Create database.

假设我要创建一个数据库而不指定任何其他文件组。 现在,双击创建数据库。

View Parameters

It contains a script for the drop database if it already exists and creates a database without specifying the data and log file directories. By default, SQL Server creates a copy of the model database and creates a database in the configured default directories.

它包含一个放置数据库的脚本(如果已存在),并创建一个数据库而不指定数据和日志文件目录。 默认情况下,SQL Server创建模型数据库的副本,并在配置的默认目录中创建数据库。

In the above scripts, we need to specify database name three times. We can either manually enter the value of the variable or use a template value method that we will explore shortly in this article.

在以上脚本中,我们需要指定3次数据库名称。 我们可以手动输入变量的值,也可以使用模板值方法,我们将在本文中对此进行探讨。

Let’s close this template and open another template to create a database with multiple filegroups. It is a lengthy script, and you might feel difficult to enter the variable values. You require to go through each line of code and provide value for the variable. Annoying, Right!

让我们关闭此模板,然后打开另一个模板以创建具有多个文件组的数据库。 这是一个冗长的脚本,您可能会觉得很难输入变量值。 您需要遍历每一行代码,并为变量提供值。 烦人,对!

Create a database with multiple filegroups

指定模板参数的值
(Specify Values for Template Parameters
)

SSMS solves the problem shown above with a simple configuration. Navigate to Query-> Specify Values for Template Parameters.

SSMS通过简单的配置解决了上面显示的问题。 导航到“ 查询”->“指定模板参数的值”。

You can also use keyboard shortcut key Ctrl+Shift+M.

您也可以使用键盘快捷键Ctrl + Shift + M。

Specify Values for Template Parameters.

It opens a pop-up with a list of used parameters, its type, and values. You can also see it highlights all variables with a background color.

它会打开一个弹出窗口,其中包含已用参数,其类型和值的列表。 您还可以看到它以背景色突出显示了所有变量。

View parameters

Specify values for these parameters in this parameter box. You only need to specify the value for a parameter once, and SSMS automatically replaces the parameter variables with the value specified. This way, we do not need to go through each line of code, look for the variable, and assign value. It also saves time and eliminates human errors.

在此参数框中指定这些参数的值。 您只需为参数指定一次值,SSMS就会自动用指定的值替换参数变量。 这样,我们无需遍历代码的每一行,查找变量并分配值。 它还可以节省时间并消除人为错误。

Specify parameters value

Click OK, and it replaces the variables with the value we specified in the previous step.

单击确定,它会将变量替换为我们在上一步中指定的值。

View modified script

You can run the script directly, and it creates a database for you. It simplified your task, isn’t it! You specified only a few parameters values, and your script is ready.

您可以直接运行该脚本,它会为您创建一个数据库。 它简化了您的任务,不是吗! 您仅指定了几个参数值,并且脚本已准备就绪。

Let’s take another example of creating a stored procedure using the highlighted template.

让我们再举一个使用突出显示的模板创建存储过程的示例。

Stored procedure

Launch this template and view the query with variables. In a stored procedure, we define the variable and its data types such as int, varchar(), char, float.

启动此模板并查看带有变量的查询。 在存储过程中,我们定义变量及其数据类型,例如int,varchar(),char,float。

In the below template, we see that it asks for data types of both variables @param1 and @param2.

在下面的模板中,我们看到它要求两个变量@ param1和@ param2的数据类型。

Variable and data types

Now, launch the specify values for template parameters, as shown below. Here, we see that it defines variable data types. By default, it uses int data type.

现在,启动模板参数的指定值,如下所示。 在这里,我们看到它定义了可变数据类型。 默认情况下,它使用int数据类型。

Change data type

We can change the data type as per our requirement. For example, let’s change the data type for both variables to tinyint. It is a text box, and you can write the data type. It should be a valid data type as per the SQL Server version.

我们可以根据需要更改数据类型。 例如,让我们将两个变量的数据类型都更改为tinyint。 它是一个文本框,您可以编写数据类型。 根据SQL Server版本,它应该是有效的数据类型。

View modified data type

Click OK, and you can see changes in the script.

Verify changes

单击确定,您可以在脚本中看到更改。

创建用户定义的模板 (Create a User-defined template )

SSMS allows you to define your script templates. It is an excellent way to define your scripts and frequently used scripts such as procedure, function, views. For this demo, I am using the alter stored procedure template code and saving it as a new custom template.

SSMS允许您定义脚本模板。 这是定义脚本和常用脚本(如过程,函数,视图)的绝佳方法。 对于此演示,我正在使用alter存储过程模板代码并将其另存为新的自定义模板。

Suppose you want to create a template to alter a stored procedure. Right-click on the Stored Procedure folder and choose to select a new Template. You can create a new folder as well.

假设您要创建一个模板来更改存储过程。 右键单击“ 存储 过程”文件夹,然后选择一个新模板 。 您也可以创建一个新文件夹。

Create a User-defined template

It creates a blank template inside the folder. Give it an appropriate name, as shown below.

它在文件夹内创建一个空白模板。 给它起一个适当的名称,如下所示。

Rename template

Right-click on this template and click on Edit. Write the code in the template window as per your requirements.

右键单击此模板,然后单击“编辑”。 根据您的要求在模板窗口中编写代码。

Edit user-defined template

Save this template using Ctrl + S, and you can use it as other system templates.

使用Ctrl + S保存此模板,您可以将其用作其他系统模板。

You should also explore Analysis Services templates for DAX, DMX, and MDX queries.

您还应该探索用于DAX,DMX和MDX查询的Analysis Services模板。

Analysis Services templates

结论
(Conclusion
)

SQL Server script templates provide a powerful utility in SQL Server Management Studio (SSMS). We can use it as a repository for frequently used queries with variables. It is easy to use and design for user-defined templates as well. I recommend you to take a look and use them if you find it suitable for your needs.

SQL Server脚本模板在SQL Server Management Studio(SSMS)中提供了强大的实用程序。 我们可以将其用作经常使用的带有变量的查询的存储库。 用户定义模板也易于使用和设计。 如果您认为它适合您,我建议您看看并使用它们。

翻译自: https://www.sqlshack.com/script-templates-in-sql-server-management-studio-ssms/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值