创建和使用代码片段来快速创建中的 TRANSACT-SQL (T-SQL) 脚本 Azure Data StudioAzure Data StudioCreate and use code snippets to quickly create Transact-SQL (T-SQL) scripts in Azure Data StudioAzure Data Studio

中的代码片段Azure Data StudioAzure Data Studio是模板,可以轻松快速地创建数据库和数据库对象。Code snippets in Azure Data StudioAzure Data Studio are templates that make it easy to create databases and database objects.

Azure Data StudioAzure Data Studio 提供了若干 T-SQL 代码片段来帮助你快速生成正确的语法。provides several T-SQL snippets to assist you with quickly generating the proper syntax.

也可以创建用户定义的代码片段。User-defined code snippets can also be created.

使用内置的 T-SQL 代码片段Using built-in T-SQL code snippets

  1. 若要访问可用的代码片段,请键入sql在查询编辑器中打开的列表:To access the available snippets, type sql in the query editor to open the list:

    代码段

  2. 选择你想要使用,代码的段,并生成 T-SQL 脚本。Select the snippet you want to use, and it generates the T-SQL script. 例如,选择sqlCreateTable:For example, select sqlCreateTable:

    创建表的代码段

  3. 具有特定值更新突出显示的字段。Update the highlighted fields with your specific values. 例如,替换TableName架构替换为你的数据库的值:For example, replace TableName and Schema with the values for your database:

    替换模板字段

    如果你想要更改的字段不能再突出显示 (发生这种情况时将光标在编辑器中四处移动),右键单击你想要更改,并选择的词语更改所有匹配项:If the field you want to change is no longer highlighted (this happens when moving the cursor around the editor), right-click the word you want to change, and select Change all occurrences:

    替换模板字段

  4. 更新或添加任何其他 T-SQL 的所需的所选的代码段。Update or add any additional T-SQL you need for the selected snippet. 例如,更新Column1Column2,并添加更多的列。For example, update Column1, Column2, and add more columns.

创建 SQL 代码片段Creating SQL code snippets

您可以定义自己的代码段。You can define your own snippets. 若要打开的 SQL 代码段文件进行编辑:To open up the SQL snippet file for editing:

  1. 打开命令面板(Shift + Ctrl + P),然后键入截图,并选择首选项:打开用户代码片段:Open the Command Palette (Shift+Ctrl+P), and type snip, and select Preferences: Open User Snippets:

    替换模板字段

  2. 选择SQL:Select SQL:

    备注

    Azure Data StudioAzure Data Studio 从 Visual Studio Code 继承其代码段功能,因此本文专门讨论如何使用 SQL 代码段。inherits its code snippet functionality from Visual Studio Code so this article specifically discusses using SQL snippets. 有关详细信息,请参阅创建自己的代码段Visual Studio Code 文档中。For more detailed information, see Creating your own snippets in the Visual Studio Code documentation.

    替换模板字段

  3. 粘贴下面的代码插入sql.json:Paste the following code into sql.json:

    {
    "Select top 5": {
     "prefix": "sqlSelectTop5",
     "body": "SELECT TOP 5 * FROM ${1:TableName}",
     "description": "User-defined snippet example 1"
     },
     "Create Table snippet":{
     "prefix": "sqlCreateTable2",
     "body": [
     "-- Create a new table called '${1:TableName}' in schema '${2:SchemaName}'",
     "-- Drop the table if it already exists",
     "IF OBJECT_ID('$2.$1', 'U') IS NOT NULL",
     "DROP TABLE $2.$1",
     "GO",
     "-- Create the table in the specified schema",
     "CREATE TABLE $2.$1",
     "(",
     "   $1Id INT NOT NULL PRIMARY KEY, -- primary key column",
     "   Column1 [NVARCHAR](50) NOT NULL,",
     "   Column2 [NVARCHAR](50) NOT NULL",
     "   -- specify more columns here",
     ");",
     "GO"
     ],
    "description": "User-defined snippet example 2"
    }
    }
    
  4. 保存 sql.json 文件。Save the sql.json file.

  5. 通过单击打开新查询编辑器窗口Ctrl + NOpen a new query editor window by clicking Ctrl+N.

  6. 类型sql,并查看您刚添加的; 两个用户代码片段sqlCreateTable2sqlSelectTop5Type sql, and you see the two user snippets you just added; sqlCreateTable2 and sqlSelectTop5.

选择一个新的代码段和对其进行测试运行 !Select one of the new snippets and give it a test run!

其他资源Additional resources

有关 SQL 编辑器的信息,请参阅代码编辑器教程For information about the SQL editor, see Code editor tutorial.