sql azure 语法

This article will fully cover the code snippet SQL developer productivity feature in Azure Data Studio including a list of available snippets and examples of how to create custom code snippets

本文将全面介绍Azure Data Studio中的代码片段SQL开发人员生产力功能,包括可用片段列表以及如何创建自定义代码片段的示例

Azure Data Studio (initially called SQL Operations Studio) is a new client tool for SQL Server that works on the cross-platform tool. Starting from SQL Server 2017, we can install SQL Server on the Linux operating system as well. Usually, we use SSMS to connect with SQL Server, but you cannot install SSMS in Linux OS. Azure Data Studio is a development tool that you can run on Linux as well. Microsoft provides monthly release of Azure Data Studio for new features, bug fixes and enhancement over existing features.

Azure Data Studio(最初称为SQL Operations Studio)是适用于跨平台工具SQL Server的新客户端工具。 从SQL Server 2017开始,我们也可以在Linux操作系统上安装SQL Server。 通常,我们使用SSMS与SQL Server连接,但是您不能在Linux OS中安装SSMS。 Azure Data Studio是一种开发工具,您也可以在Linux上运行。 Microsoft每月提供Azure Data Studio版本,以提供新功能,错误修复和对现有功能的增强。

You can download current release version 1.5.2(Release date March 22, 2019) from Microsoft Docs. Download Azure Data Studio on a suitable platform (Windows/MacOS/Linux) to work with SQL Server.

您可以从Microsoft Docs下载当前发行版本1.5.2(发布日期为2019年3月22日)。 在合适的平台(Windows / MacOS / Linux)上下载Azure Data Studio以与SQL Server一起使用。

A few essential features of Azure Data Studio are as follows

Azure Data Studio的一些基本功能如下

  • Modern query editor with fast IntelliSense, Code Snippets and Source control integration 具有快速IntelliSense,代码段和源代码控制集成的现代查询编辑器
  • Marketplace Extensions to get new custom features 市场扩展获得新的自定义功能
  • Integrated terminal for PowerShell, SSH or command prompt PowerShell,SSH或命令提示符的集成终端
  • Customizable dashboards 可定制的仪表板
  • Built-in charts for the query results 查询结果的内置图表

We use many custom scripts in SQL Server for database development, monitoring and administrative purpose. Usually, we store scripts in a folder and browse to the folder as per requirement. You can also place scripts on a shared drive so that team members can also use it. For a large number of scripts, we may need to open individual files to get a code preview.

我们在SQL Server中使用许多自定义脚本来进行数据库开发,监视和管理。 通常,我们将脚本存储在一个文件夹中,然后根据需要浏览到文件夹。 您还可以将脚本放置在共享驱动器上,以便团队成员也可以使用它。 对于大量脚本,我们可能需要打开单个文件才能获得代码预览。

In SSMS, we have Code Snippets available in terms of templates. For example, connect to a SQL instance and expand Databases.

在SSMS中,我们提供了有关模板的代码段。 例如,连接到SQL实例并展开Databases

Suppose we want to create a new stored procedure. We are not aware of the syntax for the stored procedures. We can right-click on Stored Procedures and then on New.

假设我们要创建一个新的存储过程。 我们不知道存储过程的语法。 我们可以右键单击“存储过程”,然后单击“ 新建”

Once you click on a Stored Procedure, it opens a template to write a stored procedure. In the following screenshot, we can add the required variables and table names to write down the procedure.

单击存储过程后,它将打开一个模板来编写存储过程。 在下面的屏幕截图中,我们可以添加所需的变量和表名称以写下该过程。

Azure Data Studio contains new features to increase productivity in writing T-SQL queries. Code Snippets is one such important features of Azure Data Studio.

Azure Data Studio包含新功能,可以提高编写T-SQL查询的效率。 代码段是Azure Data Studio如此重要的功能之一。

This article covers the following topics:

本文涵盖以下主题:

  • Overview of Code Snippets 代码段概述
  • Default Code Snippets 默认代码段
  • Create a custom Code Snippets in Azure Data Studio 在Azure Data Studio中创建自定义代码段
  • Custom Code Snippets with variables 带有变量的自定义代码片段
  • Custom Code Snippets with multiple lines of codes 具有多行代码的自定义代码段

代码段概述 (Overview of Code Snippets )

We need to run common database queries to perform various tasks in a database environment. Some of these activities can be as follows.

我们需要运行常见的数据库查询以在数据库环境中执行各种任务。 其中一些活动如下。

  • Query to create a database 查询创建数据库
  • Table creation 表格创建
  • Query format for stored procedure 存储过程的查询格式
  • Monitoring queries using dynamic management views 使用动态管理视图监视查询
  • We need to frequently view records from a table, procedures, and views. 我们需要经常从表,过程和视图中查看记录。

We can use Code Snippets in Azure Data Studio to accomplish these tasks for us. We do not need to write complete code again and again. It provides code efficiency, standardization as well as time-saving in writing queries.

我们可以使用Azure Data Studio中的代码段为我们完成这些任务。 我们不需要一次又一次地编写完整的代码。 它提供了代码效率,标准化以及节省了编写查询的时间。

Let’s explore these Code Snippets in Azure Data Studio. Right click on a connected SQL Instance in Azure Data Studio and go to New Query.

让我们探索Azure Data Studio中的这些代码段。 右键单击Azure Data Studio中已连接SQL实例,然后转到“新建查询”。

Azure Data Studio provides intelligent suggestions to help you write the queries. These snippets start with word SQL. In a new query editor, type SQL and it populates a list of available Code Snippets.

Azure Data Studio提供了智能建议,可帮助您编写查询。 这些片段以单词SQL开头。 在新的查询编辑器中,键入SQL,它将填充可用代码段的列表。

We can use Up and Down arrow keys to move through all code snippets. It also gives a brief description once you choose any Code Snippet.

我们可以使用向上和向下箭头键浏览所有代码段。 选择任何代码段后,它还会提供简短说明。

It also shows an information icon

它还显示一个信息图标

It allows reviewing the information inside the Code Snippets. Press Enter. It opens a new query editor and copies the code for the particular code Snippet in it.

它允许查看代码段中的信息。 按Enter键 。 它将打开一个新的查询编辑器,并在其中复制特定代码段的代码。

In this example, we can see the query to create a new database. It creates a new database if it does not exist in connected SQL instance. We need to enter the value of highlighted field ‘DatabaseName’ and execute this.

在此示例中,我们可以看到创建新数据库的查询。 如果连接SQL实例中不存在新数据库,它将创建一个新数据库。 我们需要输入突出显示的字段“ DatabaseName”的值并执行。

Similarly, we can use sqlGetSpaceUsed Code Snippet from the list to get space used by a specified table.

同样,我们可以从列表中使用sqlGetSpaceUsed代码片段来获取指定表使用的空间。

Azure Data Studio中的可用代码段列表 (List of available Code Snippets in Azure Data Studio)

We have various Code Snippets already present in Azure Data Studio.

我们已经在Azure Data Studio中提供了各种代码片段。

  • sqlAddColumn: Add a new column to a table sqlAddColumn :将新列添加到表
  • sqlCreateIndex: to create a new index in a table sqlCreateIndex :在表中创建新索引
  • sqlCreateStoredProd: Create a stored procedure sqlCreateStoredProd :创建存储过程
  • sqlCreateTable : Create a new table sqlCreateTable :创建一个新表
  • sqlCreateTempTable: Create a new temporary table sqlCreateTempTable :创建一个新的临时表
  • sqlCreateCursor: Declare a cursor sqlCreateCursor :声明一个游标
  • sqlDeleteRows: Delete rows from a table sqlDeleteRows :从表中删除行
  • sqlDropColumn: To drop a column from an existing table sqlDropColumn :从现有表中删除列
  • sqlDropDatabase: To drop a database sqlDropDatabase :删除数据库
  • sqlDropStoredproc: To drop a stored procedure sqlDropStoredproc :删除存储过程
  • sqlDropTable: To drop a table sqlDropTable :删除表
  • sqlInsertRows: To insert rows in a table sqlInsertRows :在表中插入行
  • sqlListDatabases: Get a list of databases sqlListDatabases :获取数据库列表
  • sqlListTablesAndViews: Get a list of tables and views in the current database sqlListTablesAndViews :获取当前数据库中的表和视图的列表
  • sqlSelect: Select rows from a table or view sqlSelect :从表或视图中选择行
  • sqlUpdate: To update rows in a table sqlUpdate :更新表中的行

Azure Data Studio allows you to create your Code Snippets as well. Suppose you execute few commands regularly. You can create your Code Snippets in Azure Data Studio. We do not need to browse the directory and search for a particular.SQL file. We can directly call it with intellisense suggestions, enter required parameters and execute it.

Azure Data Studio允许您也创建代码段。 假设您定期执行一些命令。 您可以在Azure Data Studio中创建代码段。 我们不需要浏览目录并搜索特定的.SQL文件。 我们可以通过智能建议直接调用它,输入所需参数并执行它。

Let’s explore steps to configure Code Snippets in Azure Data Studio.

让我们探索在Azure Data Studio中配置代码段的步骤。

创建自定义代码段 (Create a custom Code Snippets)

In Azure Data Studio, go to View and click on Command Pallete. You can also use short cut key Ctrl+Shift+P.

在Azure Data Studio中,转到“ 视图” ,然后单击“ 命令面板” 。 您也可以使用快捷键Ctrl + Shift + P。

In Command pallets window select Preferences: Configure User Snippets

在“命令托盘”窗口中,选择“首选项:配置用户代码段”

It opens a list of available Snippets file language format. Select the SQL option from this list.

它将打开可用的片段代码语言格式的列表。 从此列表中选择SQL选项。

It opens SQL.json file to configure SQL Code Snippets.

它将打开SQL.json文件以配置SQL代码段。

  • Each Code Snippet name should be unique 每个代码段名称应唯一
  • We have three parts of a Code Snippet 我们有一个代码片段的三个部分
  • Prefix: It is the name of a code snippet. 前缀:这是代码段的名称。
  • Body: We expand the body and insert it into the New Query window. 正文:我们展开正文并将其插入“新查询”窗口。
  • Description: We can add a custom description to understand this easily. 说明:我们可以添加自定义说明以方便地理解。

The basic syntax of a Code Snippet is as follows.

代码段的基本语法如下。

"<name of the snippet>": {
"prefix":"<sqlNameOfthePrefix>",
"body":[
"<T-SQL code line 1 ${1:DefaultValue1}>",
.
.
"< T-SQL code line N>"
],
"description":"<Snippet description>"
}

Suppose we want to select top 10 rows from a specified table. In the following code,

假设我们要从指定的表中选择前10行。 在以下代码中,

{"Select TOP 10 Rows": {"prefix": "sqlTop10Select","body": "SELECT TOP 10 * FROM ${1:SchemaName}.${2:TableName}","description": "it reads top 10 records from specified table"},}

Press Ctrl+S to save this SQL.json file. Now open another new query window and enter SQL. In the following screenshot, you can see a sqlTop10Select code snippet that we created in above step.

按Ctrl + S保存此SQL.json文件。 现在打开另一个新的查询窗口,然后输入SQL。 在以下屏幕截图中,您可以看到我们在上一步中创建的sqlTop10Select代码片段。

Press Enter and you can see query mentioned inside the snippet.

按Enter键,您可以在摘要中看到查询。

We can specify the schema and table name to execute this query.

我们可以指定模式和表名来执行此查询。

SELECT TOP 10 * FROM HumanResources.Employee

Suppose we want to know the reason for database transaction log holding reason. We can use a log_reuse_wait_desc column in sys.databases to see the information about transaction log no truncation reason. We can use the following code to create Code Snippet. Copy and paste the following code into SQL.json file.

假设我们想知道数据库事务日志保存原因。 我们可以在sys.databases中使用log_reuse_wait_desc列来查看有关事务日志无截断原因的信息。 我们可以使用以下代码来创建代码片段。 将以下代码复制并粘贴到SQL.json文件中。

"Select Log holding reason": {"prefix": "sqlLogReuse","body": "select name,log_reuse_wait_desc from sys.databases where name=${1:DatabaseName}","description": "We can get trasaction log holding reason with this snippet"},

In the following image, you can see we can configure multiple Code Snippets in Azure Data Studio.

在下图中,您可以看到我们可以在Azure Data Studio中配置多个代码段。

You can see both configured Code Snippet in the following image.

您可以在下图中看到两个已配置的代码段。

In the previous examples, we created basic snippets in Azure Data Studio. Let us do further customization in the upcoming section.

在前面的示例中,我们在Azure Data Studio中创建了基本代码段。 让我们在接下来的部分中进行进一步的自定义。

带有变量的自定义代码段 (Custom Code Snippet with Variables)

In SQL Server, we can check backup or restore completion status using a percent_complete column of DMV sys.dm_exec_requests.

在SQL Server中,我们可以使用DMV sys.dm_exec_requestspercent_complete列检查备份或还原完成状态。

--Backup status
SELECT session_id command, percent_complete
FROM sys.dm_exec_requests
WHERE command LIKE '%backup%';--Restore statusSELECT session_id command, percent_complete
FROM sys.dm_exec_requests
WHERE command LIKE '%restore%';

Suppose we want to create a Code Snippet to query backup or restore completion status. We want a drop-down list to choose from the keyword Backup or Restore. We can specify values in snippet separated by the |symbol.

假设我们要创建一个代码片段以查询备份或还原完成状态。 我们希望从关键字“备份”或“还原”中选择一个下拉列表。 我们可以在代码段中用|隔开指定值 符号。

In the following code, we define variable ‘%${1|BackUp, Restore|}%’ to choose from values specified in this.

在下面的代码中,我们定义变量' %$ {1 | BackUp,Restore |}%'以从其中指定的值中进行选择。

    "Backup or Restore Completion Status": {"prefix": "sqlBackupRestoreStatus","body": "SELECT session_id command,percent_complete FROM sys.dm_exec_requests WHERE command LIKE '%${1|BackUp,Restore|}%'","description": "We can get Backup or Restore Pecentage Completion Status"},

In the new query window, we can see this Code Snippet.

在新的查询窗口中,我们可以看到此代码段。

Click Enter and You get drop-down values to choose appropriate values.

单击Enter ,您将获得下拉值以选择适当的值。

You can use up and down arrow keys to select the required keyword. In the following screenshot, we picked keyword Restore.

您可以使用向上和向下箭头键选择所需的关键字。 在以下屏幕截图中,我们选择了关键字Restore。

Azure Data Studio中具有多行代码的自定义代码段 (Custom Code Snippet with multiple lines of codes in Azure Data Studio)

In previous examples, we created a Code Snippet with a query having one line only. Writing a single line code is easy. But suppose we have a large piece of code that we want to convert into a Code Snippet in Azure Data Studio. It is entirely possible to create it, but you need to do extra configuration in the query of a particular Code Snippet.

在前面的示例中,我们创建了一个仅包含一行查询的代码段。 编写单行代码很容易。 但是,假设我们有大量的代码要在Azure Data Studio中转换为代码段。 完全可以创建它,但是您需要在特定代码段的查询中进行额外的配置。

In the following query, we want to get records from a Select statement. We need to take care of the following things.

在以下查询中,我们希望从Select语句中获取记录。 我们需要注意以下事项。

  • If the query contains multiple lines, we need to specify square brackets in the body of Code Snippets 如果查询包含多行,我们需要在代码段的主体中指定方括号
  • Each line should contain double quotes before and after the statement 每行应在语句前后加上双引号
  • Each line should have a comma in last 每行最后应有一个逗号
{"Output of Sales query": {"prefix": "SQLSales","body":["Use [AdventureWorks2017]"."Go","SELECT s.[BusinessEntityID],p.[Title],p.[FirstName] ",",p.[MiddleName],p.[LastName],p.[Suffix],e.[JobTitle] ",",pp.[PhoneNumber],pnt.[Name] AS [PhoneNumberType] ",",ea.[EmailAddress],p.[EmailPromotion],a.[AddressLine1] ",",a.[AddressLine2] ","FROM [Sales].[SalesPerson] s ","INNER JOIN [HumanResources].[Employee] e  ","ON e.[BusinessEntityID] = s.[BusinessEntityID] ","INNER JOIN [Person].[Person] p ","ON p.[BusinessEntityID] = s.[BusinessEntityID] ","INNER JOIN [Person].[BusinessEntityAddress] bea  ","ON bea.[BusinessEntityID] = s.[BusinessEntityID]  ","INNER JOIN [Person].[Address] a  ","ON a.[AddressID] = bea.[AddressID] ","INNER JOIN [Person].[StateProvince] sp ", "ON sp.[StateProvinceID] = a.[StateProvinceID] ","INNER JOIN [Person].[CountryRegion] cr  ","ON cr.[CountryRegionCode] = sp.[CountryRegionCode] ","LEFT OUTER JOIN [Sales].[SalesTerritory] st  ","ON st.[TerritoryID] = s.[TerritoryID] ","LEFT OUTER JOIN [Person].[EmailAddress] ea ","ON ea.[BusinessEntityID] = p.[BusinessEntityID] ","LEFT OUTER JOIN [Person].[PersonPhone] pp ","ON pp.[BusinessEntityID] = p.[BusinessEntityID] ","LEFT OUTER JOIN [Person].[PhoneNumberType] pnt ","ON pnt.[PhoneNumberTypeID] = pp.[PhoneNumberTypeID]; ","Go",],},
}

Copy and Paste the code in SQL.json and save it. Now go back to the New Query window and call the Code Snippet.

将代码复制并粘贴到SQL.json中并保存。 现在返回到“新查询”窗口并调用代码段。

We can see the Code Snippet in the following screenshot.

我们可以在下面的屏幕截图中看到代码片段。

Once you click enter, you can see the complete query.

单击回车后,您可以看到完整的查询。

We can execute to get the required result set.

我们可以执行以获得所需的结果集。

结论 (Conclusion)

In this article, we explored the Code Snippet feature of Azure Data Studio. We also studied creating custom Code Snippets with variables as well. I recommend you to configure the Code Snippet feature in Azure Data Studio so that you can benefit from this great productivity feature.

在本文中,我们探讨了Azure Data Studio的代码段功能。 我们还研究了如何创建带有变量的自定义代码段。 我建议您在Azure Data Studio中配置代码段功能,以便可以从此功能强大的生产力中受益。

翻译自: https://www.sqlshack.com/sql-code-snippets-in-azure-data-studio/

sql azure 语法

sql azure 语法_Azure Data Studio中SQL代码段相关推荐

  1. sql azure 语法_Azure Data Studio中SQL Server Profiler

    sql azure 语法 In this article, we will explore SQL Server Profiler in Azure Data Studio in detail inc ...

  2. sql azure 语法_Azure Data Studio中SQL Server架构比较扩展

    sql azure 语法 This article explores the SQL Server Schema Compare extension in the Azure Data Studio. ...

  3. sql azure 语法_Azure中的新SQL数据仓库

    sql azure 语法 介绍 (Introduction) In previous chapters, we taught how to create SQL Databases in Azure. ...

  4. sql azure 语法_Azure Kubernetes服务(AKS)中SQL Server

    sql azure 语法 In this article, we will review how to create a Kubernetes cluster in Azure Kubernetes ...

  5. sql azure 语法_Azure SQL Server中的CREATE DATABASE语句概述

    sql azure 语法 In this article, we will review CREATE DATABASE statement in the Azure SQL database wit ...

  6. sql azure 语法_Azure Kubernetes服务(AKS)–管理SQL Server数据库文件

    sql azure 语法 In this article, we will review on managing database files of SQL Server running on Azu ...

  7. sql azure 语法_Azure SQL Server自动故障转移组

    sql azure 语法 In this article, we will review how to set up auto-failover groups in Azure SQL Server ...

  8. sql azure 语法_Azure SQL –使用Azure自动化的索引表

    sql azure 语法 This article provides an overview of indexing tables in Azure SQL database using Azure ...

  9. azure云数据库_Azure Data Studio中Windows的数据库管理工具扩展

    azure云数据库 Azure Data Studio provides a modern and productive experience for managing on-premise and ...

最新文章

  1. 数据的交换输出【杭电-2016】 附题
  2. Java提高篇 —— Java内部类详解
  3. bzoj1969: [Ahoi2005]LANE 航线规划(树链剖分)
  4. 查询本地内存的栈大小_Js的内存问题
  5. es创建索引库报错 :Types cannot be provided in put mapping requests, unless the include_type_na
  6. 找不到ffmpeg.dll无法继续执行代码怎么办_2020年,小规模增值税3%减按1%征收,那么一般纳税人该怎么办?...
  7. 大数据发展的根基是什么?
  8. 使用CImageList的一点心得
  9. Python题目:个人所得税计算
  10. node项目报错had too many unstable restarts (16). Stopped. “errored”
  11. Oracle数据库上机练习6
  12. jQuery插件开发标准写法
  13. Chloe.Orm多表连接查询 (二)
  14. C#-----集合ListT的常用方法
  15. 力扣刷题 DAY_64 回溯
  16. snake与LunarLander源代码分析
  17. 论文复现_1:Chinese NER Using Lattice LSTM
  18. 应用程序日志管理工具
  19. 怎么用python做战斗机_少儿编程分享:手把手教你用Python编写战斗机游戏(四)
  20. 联系书商出译著的流程

热门文章

  1. dos怎么退出debug_电脑主板坏了怎么办?浅谈主板常见的故障以及应对办法
  2. 陷阱房图纸_揭秘户型图 | 研究了100个户型图后,我发现了这4个重大陷阱
  3. pygame-KidsCanCode系列jumpy-part6-主角挂掉重新开始
  4. hibernate入门实例
  5. 【codevs1034】家园——网络流
  6. Easyui在form表单提交的时候,如果有datebox报 Uncaught SyntaxError: Unexpected token 的解决...
  7. li中浮动元素span等在IE和Firefox中的高度Bug
  8. LeetCode(530)——二叉搜索树的最小绝对差(JavaScript)
  9. Error:Comments are not permitted in JSON
  10. 零基础带你学习MySQL—加密函数和系统函数(十六)