azure上传代码_深入了解Azure Data Studio:更多代码和更少GUI

azure上传代码

In the previous two articles, Starting your journey with Azure Data Studio and Developing your SQL Server database using Azure Data Studio, we introduced the Azure Data Studio tool by describing the main components of that data studio and showed how we can take advantages from it in developing and organizing the database related projects.

在前两篇文章《 开始使用Azure Data Studio使用Azure Data Studio 开发SQL Server数据库》中 ,我们通过描述该Data Studio的主要组件并介绍了如何利用它来介绍Azure Data Studio工具。开发和组织与数据库相关的项目。

In this article, we will go deeper and present the advanced tasks that can be performed using the Azure Data Studio.

在本文中,我们将更深入地介绍可以使用Azure Data Studio执行的高级任务。

在你开始前 (Before you start)

Azure Data Studio is a dynamic tool that is reviewed continuously based on the users’ feedback. Also, it introduced many features that are still under preview and updated frequently. In order to keep the version that is installed on your machine up-to-date and with the latest features and the bug fixes, it is recommended to update it continuously.

Azure Data Studio是一个动态工具,可以根据用户的反馈对其进行连续审查。 此外,它引入了许多功能,这些功能仍在预览中并经常更新。 为了使计算机上安装的版本保持最新,并具有最新功能和错误修复,建议不断对其进行更新。

To update the Azure Data Studio, open the tool and choose the Restart to Update option from the Help menu, and the studio will be closed, updated with the latest changes then opened automatically, as shown below:

若要更新Azure Data Studio,请打开该工具,然后从“ 帮助”菜单中选择“ 重新启动以更新”选项,然后该工作室将关闭,使用最新更改进行更新,然后自动打开,如下所示:

Restart to Update

After updating Azure Data Studio, it is recommended to organize SQL Server instances in your environment within groups. This will make it easier for you to directly identify the server that you manage to use in your project. For example, you can create a group for all SQL Server instances in the development environment, another servers group for the testing environment, and a restricted group for the production environment.

更新Azure Data Studio之后,建议将您的环境中SQL Server实例组织在组中。 这将使您更容易直接识别要在项目中使用的服务器。 例如,您可以为开发环境中的所有SQL Server实例创建一个组,为测试环境创建另一个服务器组,为生产环境创建一个受限组。

To achieve that, click on the New Server Group button, under the Connections tab, as below:

为此,请单击“ 连接”选项卡下的“ 新建服务器组”按钮,如下所示:

New Server Group

From the Add server group window, provide the name and the description for that server group in addition to color to distinguish that group quickly. In the example below, we will create three servers’ groups: Dev, UAT, and Prod:

在“ 添加服务器组”窗口中,除了提供颜色外,还要提供该服务器组的名称和描述,以快速区分该组。 在下面的示例中,我们将创建三个服务器组:Dev,UAT和Prod:

Add Server Group

Now the server groups are created successfully. To add servers to these groups, choose the related server group name from the group’s drop-down list, during the connection creation process, or modify the recent or the saved connections with the correct group name, and the servers will be displayed directly under the corresponding group when connecting to that server:

现在,服务器组已成功创建。 要将服务器添加到这些组中,请在连接创建过程中从组的下拉列表中选择相关的服务器组名称,或使用正确的组名称修改最近的或保存的连接,服务器将直接显示在连接到该服务器时对应的组:

Add Servers to Groups

开发存储过程 (Developing stored procedures)

Azure Data Studio contains a rich query editor that allows us to develop any database object easily with a few clicks. In the previous article, we showed how to create a new database, a new table, and modify the table’s data without the need to be a skilled database developer.

Azure Data Studio包含一个丰富的查询编辑器,使我们能够通过单击几下轻松地开发任何数据库对象。 在上一篇文章中,我们展示了如何创建新数据库,新表以及如何修改表的数据,而无需成为熟练的数据库开发人员。

In this example, we will show how to develop a new stored procedure.

在此示例中,我们将显示如何开发新的存储过程。

The first step here is to connect to the SQL Server instance that hosts the database where the stored procedure will be created. This can be achieved by different methods like choosing to Create a Connection from the Welcome page, and provide the name of the SQL Server instance, the authentication method and the credentials that will be used to connect to that SQL Server, as below:

第一步是连接到承载将在其中创建存储过程的数据库SQL Server实例。 这可以通过不同的方法来实现,例如从“欢迎”页面选择“ 创建连接 ”,并提供SQL Server实例的名称,身份验证方法和将用于连接到该SQL Server的凭据,如下所示:

Create a Connection

After connecting to the SQL Server instance, open the query editor window by choosing New Query option from the File menu, or simply right-clicking on the database that you are interested in creating the stored procedure and choose New Query option, as indicated below:

连接到SQL Server实例后,从文件菜单中选择新建查询选项打开查询编辑器窗口,或者只需右键点击你有兴趣创建存储过程,并选择新建查询选项,如下文所示的数据库:

New Query

If the query editor window is still showed as disconnected, click on the Connect option and select the name of the connection that represents the SQL Server instance that hosts your database.

如果查询编辑器窗口仍然显示为已断开连接,请单击“ 连接”选项,然后选择代表承载数据库SQL Server实例的连接的名称。

To create a new stored procedure, you can take advantage of the built-in IntelliSense feature that automatically completes the statement you are trying to write or the snippets code feature that provides you with templates for the object you manage to create.

要创建新的存储过程,您可以利用内置的IntelliSense功能(该功能自动完成您要编写的语句)或摘要代码功能(该功能为您提供要创建的对象的模板)。

In our demo here, we will use the “Create a Stored procedure” snippet to provide us with the template that can make it easier for us to create that stored procedure and fill only the basic information without recalling the full syntax for the CREATE PROC T-SQL statement.

在这里的演示中,我们将使用“创建存储过程”代码段为我们提供模板,该模板可以使我们更轻松地创建该存储过程并仅填充基本信息,而无需调用CREATE PROC T的完整语法。 -SQL语句。

In the below snippet, you will see that the script will be in the form of “drop, create then execute”. Where it will check first if there is any stored procedure with the same name and drop it before creating the new one then provide the statement that is used to execute that stored procedure for verification purposes, as shown below:

在下面的代码片段中,您将看到脚本的形式为“删除,创建然后执行”。 它将首先检查是否存在任何同名存储过程,并在创建新存储过程之前将其删除,然后提供用于执行该存储过程的语句以进行验证,如下所示:

CREATE PROC Sinppet

In addition to the useful template, the Azure Data Studio query editor is clever in a way that helps to change all occurrences for a specific statement in the whole document. This will help if you plan to change the default name of the stored procedure wherever it is used.

除了有用的模板之外,Azure Data Studio查询编辑器还很聪明,可以帮助更改整个文档中特定语句的所有匹配项。 如果您计划更改使用存储过程的默认名称,这将有所帮助。

This can be achieved by selecting the text that you plan to change, right-click on that text, and choose Change All Occurrences option. When you write the new name for the stored procedure, you will see that the cursor is pointing to all text occurrences and the value is changing simultaneously in all cursor locations:

这可以通过选择计划更改的文本,右键单击该文本并选择“ 更改所有出现次数”选项来实现。 当为存储过程编写新名称时,您将看到光标指向所有出现的文本,并且值在所有光标位置同时改变:

Change All Occurrences

If you are still not surprised by the functionalities of this query editor, Azure Data Studio will make you astonished with the Peek Definition feature.

如果您对该查询编辑器的功能仍然不感到惊讶,则Azure Data Studio将使您对Peek Definition功能感到惊讶。

When we create a stored procedure that is reading from a table that contains a large number of columns or reading from multiple tables and joins between these tables, it is not easy to recall the name of all the columns, the constraints on these columns, and the relations between the tables. Not to mention that visiting the schema of the table is not user-friendly.

当我们创建一个从包含大量列的表中读取或从多个表中读取并在这些表之间进行联接的存储过程时,要想起所有列的名称,这些列的约束以及表之间的关系。 更不用说访问表的架构不是用户友好的。

I agree with you that the IntelliSense feature is helpful to get the name of the tables and the columns, but this requires from you to remember part of the names to get the complete name automatically, as can be seen below:

我同意您的看法,IntelliSense功能有助于获取表和列的名称,但这需要您记住部分名称才能自动获取完整名称,如下所示:

IntelliSense

To overcome this issue, Azure Data Studio introduces a feature that helps you open the script that is used to create a specific database object as a pop-up in the same window where you are developing your stored procedure. This is similar to the script that is generating when trying to “Script as Create” for a specific table.

为解决此问题,Azure Data Studio引入了一项功能,可帮助您在开发存储过程的同一窗口中打开用于创建特定数据库对象的脚本作为弹出窗口。 这类似于尝试为特定表“创建时编写脚本”时生成的脚本。

Assume that we plan to use the Employee table in the stored procedure that we are developing. Also, this table contains a large number of columns that do not follow a specific naming convention. So, it is not easy to recall these names.

假设我们计划在我们正在开发的存储过程中使用Employee表。 此外,此表包含大量不遵循特定命名约定的列。 因此,要记住这些名称并不容易。

To display the schema of that table, including the name of the columns, the data types, the constraints and relations, right-click on the name of that table in your stored procedure and choose to Peek Definition option from the Peek options list, and a dark blue pop-up window will be displayed, that contains the schema definition for that table, where you can easily read it and use the displayed information to write your query in the stored procedure at the same time:

要显示该表的架构,包括列名,数据类型,约束和关系,请在存储过程中右键单击该表的名称,然后从“ Peek”选项列表中选择“ Peek Definition”选项,然后将显示一个深蓝色的弹出窗口,其中包含该表的架构定义,您可以在其中轻松读取表并使用显示的信息同时在存储过程中编写查询:

Peek Definition

The Go to Definition option in the previous window is helpful when you want to check the definition of another database object, such as stored procedure or function, that is used in that script, without the need to browse it under the database objects list.

如果要检查该脚本中使用的另一个数据库对象(例如存储过程或函数)的定义,而无需在数据库对象列表下浏览它,则可以使用前一个窗口中的“ 转到定义”选项。

Once you finish writing your stored procedure script, click on the Run option to execute the CREATE PROC statement that will create the stored procedure under the selected database, as you can see below:

一旦完成存储过程脚本的编写,请单击“ 运行”选项以执行CREATE PROC语句,该语句将在所选数据库下创建存储过程,如下所示:

create the stored procedure

To modify any stored procedure, view or function under a specific database, right-click on that database object, and choose Script as Alter option, where a new window with ALTER PROC script for that object will be displayed, allowing you to modify it as per your requirements.

要修改特定数据库下的任何存储过程,视图或函数,请右键单击该数据库对象,然后选择“ 脚本作为Alter”选项,在该窗口中将显示一个带有该对象的ALTER PROC脚本的新窗口,您可以将其修改为根据您的要求。

Other useful options displayed in the same list is generating script to create that stored procedure in order to create it in another database or server, script as drop option to generate a T-SQL statement used to drop that procedure and script as execute option to generate the T-SQL statement that can be used to execute that procedure:

在同一列表中显示的其他有用的选项是生成脚本以创建该存储过程以便在另一个数据库或服务器中创建它,脚本作为drop选项来生成用于删除该过程的T-SQL语句,脚本作为execute选项来生成可用于执行该过程的T-SQL语句:

Script as Alter

安全开发 (Security development)

We mentioned in the previous articles that the Azure Data Studio is not a database administration tool, and it provides no GUI for most of the administration tasks.

我们在之前的文章中提到过,Azure Data Studio不是数据库管理工具,并且它不为大多数管理任务提供GUI。

For instance, to create a new server login, a database user, or grant or revoke permission on a specific database object for that user, you need to write a T-SQL script to achieve that.

例如,要创建一个新的服务器登录名,一个数据库用户,或为该用户授予或撤销对特定数据库对象的权限,您需要编写一个T-SQL脚本来实现。

Again, with the help of the IntelliSense and Snippet code features, this task is not hard. For example, the script below can be used to create a new SQL Server Authentication login, map the user to a specific database and grant him owner permission on that database, as demonstrated below:

同样,借助IntelliSense和Snippet代码功能,此任务并不难。 例如,下面的脚本可用于创建新SQL Server身份验证登录名,将用户映射到特定数据库并向该用户授予对该数据库的所有者权限,如下所示:

Security development

SQLCMD (SQLCMD)

The query editor of Azure Data Studio provides us with the ability to use the interactive sqlcmd command-line utility to write Windows system commands and T-SQL statements in the same script.

Azure Data Studio的查询编辑器使我们能够使用交互式sqlcmd命令行实用程序在同一脚本中编写Windows系统命令和T-SQL语句。

To achieve that, click on the Enable SQLCMD option at the top left side of the query editor and write your “hybrid” code that contains Windows and T-SQL statements, and it will be parsed and executed successfully:

为此,请单击查询编辑器左上角的“ 启用S​​QLCMD”选项,然后编写包含Windows和T-SQL语句的“混合”代码,它将成功解析并执行:

SQLCMD

电源外壳 (PowerShell)

Yes! Rather than opening a separate console to execute PowerShell commands, Azure Data Studio can be used to run these commands directly via the studio terminal.

是! 无需打开单独的控制台来执行PowerShell命令,而是可以使用Azure Data Studio通过Studio终端直接运行这些命令。

The terminal window can be opened by choosing the Terminal option under the View menu, then used to write and execute your PowerShell commands directly, as you can observe below:

可以通过选择“ 视图”菜单下的“ 终端”选项来打开终端窗口,然后直接用于编写和执行PowerShell命令,如下所示:

PowerShell

备份还原 (Backup and restore)

The responsibility of performing, tracking, and monitoring the backup and restore process is on the database administrators. But the database developers, who are performing changes to the database schema and data, at least in the development environment, need to perform backup and restore the database frequently.

执行,跟踪和监视备份和还原过程的责任在于数据库管理员。 但是,至少在开发环境中要对数据库架构和数据进行更改的数据库开发人员需要经常执行备份和还原数据库。

Fortunately, Azure Data Studio provides us with the ability to manually perform the backup and restore operations directly from the same studio, using a backup and restore wizards very similar to the ones we are familiar with in the SSMS tool.

幸运的是,Azure Data Studio使我们能够使用非常类似于我们在SSMS工具中熟悉的备份和还原向导,直接从同一工作室手动执行备份和还原操作。

To take a new backup for a specific database, right-click on that database, and choose the Backup option. From the Backup database wizard, specify the name, type, and properties of the backup process then click on Backup option to take a backup or generate a script to perform the backup later, as is shown:

要为特定数据库进行新备份,请右键单击该数据库,然后选择“ 备份”选项。 在“ 备份数据库”向导中,指定备份过程的名称,类型和属性,然后单击“ 备份”选项以进行备份或生成脚本以稍后执行备份,如下所示:

Backup

Once the backup operation is completed, based on the displayed Tasks window with the backup operation process, you can restore that backup file to another SQL Server instance, or the same instance with another name, by right-clicking on the SQL Server instance name and choose to Restore option, then choose the location of the backup file and the restore options, similar to the SSMS ones, and click on the Restore option to start the restoration process, or generate script to restore the database later.

备份操作完成后,根据显示的带有备份操作过程的“ 任务”窗口,您可以通过右键单击SQL Server实例名称,将备份文件还原到另一个SQL Server实例或具有另一个名称的同一实例。选择“还原”选项,然后选择备份文件的位置以及与SSMS类似的还原选项,然后单击“ 还原”选项以启动还原过程,或生成脚本以稍后还原数据库。

You can monitor the progress of the restore operation too from the automatically displayed Tasks window, as you can see below:

您也可以从自动显示的“ 任务”窗口中监视还原操作的进度,如下所示:

Restore

The history of all backup operations performed on a specific SQL Server instance, which contains useful information for the database developers to monitor the versioning of the databases during their development process, can be tracked from the Home page of that SQL Server instance, that can be displayed by right-clicking on the SQL Server instance and choose the Manage option.

可以从该SQL Server实例的主页上跟踪在特定SQL Server实例上执行的所有备份操作的历史记录,其中包含有用的信息,供数据库开发人员在数据库开发过程中监视数据库的版本控制。通过在SQL Server实例上单击鼠标右键并选择“ 管理”选项来显示。

The backup status summary window displays statistical information about the backup processes that are performed for the databases hosted in that instance. To get detailed information, click on the three dots beside in that summary box and review the displayed insight:

备份状态摘要窗口显示有关对该实例中托管的数据库执行的备份过程的统计信息。 要获取详细信息,请单击该摘要框中旁边的三个点并查看显示的见解:

Backup Info

结论 (Conclusion)

In this article, we went through many useful functionalities that are provided by the Azure Data Studio to make our database development tasks easier and more organized.

在本文中,我们介绍了Azure Data Studio提供的许多有用的功能,以使我们的数据库开发任务更轻松,更有条理。

In the next article, we will see how to extend the built-in functionalities of the Azure Data Studio and how to manage an Azure SQL Database within that data studio. Stay tuned!

在下一篇文章中,我们将看到如何扩展Azure Data Studio的内置功能以及如何在该Data Studio中管理Azure SQL数据库。 敬请关注!

翻译自: https://www.sqlshack.com/digging-deeper-into-azure-data-studio-more-code-and-less-gui/

azure上传代码