ssms脚本生成缓慢

This article explores different ways to generate data scripts using Azure Data Studio and SQL Server Management Studio.

本文探讨了使用Azure Data Studio和SQL Server Management Studio生成数据脚本的不同方法。

介绍 (Introduction)

Each organization follows the standard practice of having multiple environments for a database. Usually, we use Production, UAT, Staging, Development terminology for these environments. We get different kinds of requirements for data to refresh from one environment to another such as follows:

每个组织都遵循为数据库提供多个环境的标准做法。 通常,对于这些环境,我们使用生产,UAT,登台,开发术语。 对于数据从一种环境刷新到另一种环境,我们有不同的要求,如下所示:

  • Refresh a complete production database into a lower environment 将完整的生产数据库刷新到较低的环境
  • Refresh only specific tables from production to dev, staging environment 从生产到开发,暂存环境仅刷新特定的表
  • Generate scripts (Insert statements) for table data 生成表数据的脚本(Insert语句)
  • Generate script for output of a select statement(specific data) 生成用于选择语句输出的脚本(特定数据)

In the following articles, we explored different ways of scripting objects in SQL Server:

在以下文章中,我们探讨了在SQL Server中编写对象脚本的不同方法:

  • Script SQL Server objects using DBATools 使用DBATools编写SQL Server对象脚本
  • All about MSSQL-Scripter, the SQL Server cross-platform scripting Tool 有关MSSQL-Scripter(SQL Server跨平台脚本工具)的全部信息

In this article, we will explore generating scripts using SQL Server Management Studio and Azure Data Studio.

在本文中,我们将探索使用SQL Server Management Studio和Azure Data Studio生成脚本。

SQL Server Management Studio中的“生成脚本”向导 (Generate Scripts wizard in SQL Server Management Studio)

We get the Generate Scripts wizard option in the task menu of a SQL database, as shown below:

我们在SQL数据库的任务菜单中获得了Generate Scripts向导选项,如下所示:

Select the specific table for which we need the script. We can select multiple objects as well for scripting:

选择我们需要脚本的特定表。 我们还可以选择多个对象来编写脚本:

In the next page, you’ll find scripting options like:

在下一页中,您将找到以下脚本选项:

  1. Save to file 保存到文件
  2. Save to Clipboard 保存到剪贴板
  3. Save to a new query window 保存到新的查询窗口

Click on Advanced (4) and view advanced scripting options:

单击高级 (4)并查看高级脚本选项:

I require to generate scripts for data only. We do not want object scripts in the generated script. For this requirement, make the following changes in the advanced scripting options:

我需要只为数据生成脚本。 我们不希望在生成的脚本中使用对象脚本。 为此,请在高级脚本选项中进行以下更改:

  • Script defaults: False 脚本默认值:False
  • Script extended properties: False 脚本扩展属性:False
  • Type of data to script: Data only 要编写脚本的数据类型:仅数据
  • Script check constraints: False 脚本检查约束:False
  • Script foreign keys: False 脚本外键:False
  • Script primary keys: False 脚本主键:False
  • Script Unique keys: False 脚本唯一键:False

Click OK and generate script in the new query window. Once all tasks completed in the wizard, you can see the success status of each one:

单击确定,然后在新的查询窗口中生成脚本。 在向导中完成所有任务后,您可以看到每个任务的成功状态:

It generates the Insert statement for all data rows. In the following image, we see a glimpse of insert statements:

它为所有数据行生成Insert语句。 在下图中,我们可以看到插入语句:

We can use this insert statement for data insertion in other environments. It solves the requirement if we want a script for all data rows.

我们可以使用此插入语句在其他环境中进行数据插入。 如果我们想要一个用于所有数据行的脚本,它将满足要求。

We cannot filter the rows in the Generate Script wizard of SSMS. Let’s explore the solution using Azure Data Studio.

我们无法在SSMS的“生成脚本”向导中过滤行。 让我们使用Azure Data Studio探索解决方案。

Azure Data Studio(ADS)中的简单数据脚本编写器 (Simple Data Scripter in Azure Data Studio (ADS))

In the previous articles, we have explored various solutions in the ADS extensions. An extension is ADS ability for feature enhancements. It also allows you to design your extension and publish it on the market place for other users.

在前面的文章中,我们探讨了ADS扩展中的各种解决方案。 扩展功能是ADS功能增强。 它还允许您设计扩展并将其发布给其他用户。

We should always use the latest version of ADS. In this article, I use November release version 1.13.1:

我们应该始终使用最新版本的ADS。 在本文中,我使用11月发行版1.13.1:

Launch Azure Data Studio and navigate to Extensions in the left-hand menu bar. We get a list of extensions so you can search for specific extension using keywords. For example, here we see a Simple Data Scripter extension:

启动Azure Data Studio并导航到左侧菜单栏中的扩展 。 我们获得了扩展名列表,因此您可以使用关键字搜索特定的扩展名。 例如,在这里我们看到一个简单的数据脚本扩展程序:

Click on Install, and it asks permissions for opening an external website:

单击“ 安装” ,它会要求您打开外部网站的权限:

Click on Open and in the GitHub page, and you see a file simple-data-scripter-0.1.0.vsix as shown below:

单击“ 打开”,然后在GitHub页面上,您会看到一个文件simple-data-scripter-0.1.0.vsix ,如下所示:

Download this VSIX file and save it on the appropriate location. In the Azure Data Studio, go to File, and select Install Extension from VSIX Package:

下载此VSIX文件并将其保存在适当的位置。 在Azure Data Studio中,转到“ 文件” ,然后选择“ 从VSIX包安装扩展”

Select previously downloaded VSIX file and install it:

选择先前下载的VSIX文件并安装它:

You get a prompt for accepting the security risk because it is a third party extension:

由于它是第三方扩展,因此会提示您接受安全风险:

Click Yes, and it quickly installs this data scripter extension:

单击 ,它会快速安装此数据脚本扩展程序:

You have to reload Azure Data Studio to activate the extension:

您必须重新加载Azure Data Studio才能激活扩展:

Click on Reload Now, and it restarts ADS. You should save any work before reloading ADS.

单击立即重新加载 ,它将重新启动ADS。 重新加载ADS之前,您应该保存所有工作。

使用数据脚本扩展生成脚本 (Generate scripts using the Data Script extension )

Connect to a SQL instance in Azure Data Studio, and you get an option Script Table Data in the right-click context menu:

连接到Azure Data Studio中SQL实例,您会在右键单击上下文菜单中获得“ 脚本表数据 ”选项

It opens the following window with a query to select all records from the selected table. In this example, we require a script for [dbo].[Employee] table, so it shows the following script:

它打开下面的查询窗口,以从所选表中选择所有记录。 在此示例中,我们需要用于[dbo]。[Employee]表的脚本,因此它显示以下脚本:

SELECT *
FROM [SQLShack].[dbo].[Employee];

We can use the following options for this pop-up:

我们可以在弹出窗口中使用以下选项:

  • Enter:- Press [Enter] to execute the script and get a script 输入:-按[Enter]执行脚本并获取脚本
  • Escape:- Press [ESC] for cancelling query execution 转义 :-按[ESC]取消查询执行

Let’s accept the default query and press Enter to view the script. In the tasks, it shows the status of scripting data for a specific object. We can also note that it took 0 seconds for generating the script:

让我们接受默认查询,然后按Enter键查看脚本。 在任务中,它显示特定对象的脚本数据状态。 我们还可以注意到生成脚本花费了0秒:

We get the script in the following screenshot. This script is in two parts.

我们在以下屏幕截图中获得了脚本。 该脚本分为两部分。

  1. Create table statement: It scripts all output columns with appropriate data types in a temporary table. By default, it comments the create table statement Create table语句:在临时表中用适当的数据类型编写所有输出列的脚本。 默认情况下,它注释创建表语句
  2. Insert statement: it generates an insert statement with all values from the source table 插入语句:它会生成一条插入语句,其中包含源表中的所有值

We can modify the temporary table with an appropriate table name, uncomment the create table statement:

我们可以使用适当的表名称修改临时表,而无需注释create table语句:

Now, click on Connect and specify an existing connection. You can also specify a new connection in case you want to execute this script on a separate instance:

现在,单击“ 连接”并指定一个现有连接。 如果要在单独的实例上执行此脚本,也可以指定一个新连接:

Once connected, select the destination database from the drop-down list:

连接后,从下拉列表中选择目标数据库:

Click on Run to execute this script. In the output, we see that six rows inserted successfully:

单击运行以执行此脚本。 在输出中,我们看到成功插入了六行:

Verify the records from the table using the SELECT statement:

使用SELECT语句验证表中的记录:

使用Azure Data Studio为选定的记录生成脚本 (Generate script for selected records using Azure Data Studio)

It is an exciting part of the data scripter extension. Suppose we have two copies of a database – Production and Test.

这是数据脚本编写器扩展中令人兴奋的一部分。 假设我们有两个数据库副本-生产和测试。

We find out that in the Production database we have records for employee id 1 to 6 while the test database has employee records up to employee id 4. In this case, we want to generate a script for employee id 5 and 6 so that we can insert it into the test database.

我们发现,在生产数据库中,我们有员工ID为1到6的记录,而测试数据库中有员工ID为4的员工记录。在这种情况下,我们要为员工ID 5和6生成一个脚本,以便我们可以将其插入测试数据库。

Right-click on the table and click Script table data. You get the following pop-up as described earlier as well:

右键点击表格,然后点击脚本表格数据 。 您还将获得如前所述的以下弹出窗口:

Click on the query and modify it to select records with employee id greater than 4 as shown below:

单击查询并对其进行修改以选择雇员ID大于4的记录,如下所示:

Press Enter and review the generated script. In the script, notice that it includes records for Employee id five and six:

Enter键并查看生成的脚本。 在脚本中,请注意,它包含员工ID 5和6的记录:

使用Azure Data Studio为目标表中的几列生成脚本 (Generate script for few columns into destination table using Azure Data Studio)

In the previous example, we generated a script for all columns of a source table and prepared a destination table. Suppose we want a few specific columns in the destination table. We can specify column names using the Data Script extension.

在前面的示例中,我们为源表的所有列生成了一个脚本,并准备了目标表。 假设我们要在目标表中添加一些特定的列。 我们可以使用数据脚本扩展名指定列名。

It is better to write the query using specific columns so that we can copy this script in the data script window:

最好使用特定列编写查询,以便我们可以在数据脚本窗口中复制此脚本:

Specify this query into the data scripter window:

在数据脚本窗口中指定此查询:

Press Enter and you get a script with only specified columns:

Enter键 ,您将获得仅包含指定列的脚本:

Generate script from the output of a view that joins multiple tables using Azure Data Studio.

使用Azure Data Studio从连接多个表的视图输出生成脚本。

We do not have options to generate a script for the query that joins multiple tables. If we right-click on a view, we do not get the option to generate the script for it:

我们没有用于为连接多个表的查询生成脚本的选项。 如果右键单击一个视图,则无法获得为其生成脚本的选项:

We can still generate insert statement scripts with a small twist. Right-click on any table in the database and click Script table data.

我们仍然可以稍作改动就生成插入语句脚本。 右键单击数据库中的任何表,然后单击“ 脚本表数据”。

In the query window, write-down script to select records from a view. This view in the sample database AdventureWorks joins multiple tables together. You can right-click on this view and script as create to view the schema of it:

在查询窗口中,写下脚本以从视图中选择记录。 示例数据库AdventureWorks中的此视图将多个表连接在一起。 您可以右键单击此视图和创建脚本,以查看其架构:

Press Enter and see our trick works! We get the view output table script and data insertion script as well:

Enter键 ,即可看到我们的技巧! 我们还获得了视图输出表脚本和数据插入脚本:

结论 (Conclusion)

In this article, we explored the data scripter extension in Azure Data Studio. We can generate scripts for objects and data insertion as well as using this. I found it a useful extension, and you should explore it using the examples specified above.

在本文中,我们探讨了Azure Data Studio中的数据脚本编写器扩展。 我们可以使用以及生成用于对象和数据插入的脚本。 我发现它是一个有用的扩展,您应该使用上面指定的示例进行探索。

翻译自: https://www.sqlshack.com/generate-data-scripts-using-ssms-and-azure-data-studio/

ssms脚本生成缓慢

ssms脚本生成缓慢_使用SSMS和Azure Data Studio生成数据脚本相关推荐

  1. azure 使用_使用Azure Data Studio开始您的旅程

    azure 使用 In this article, we will introduce the Azure Data Studio in a way that makes it easy for yo ...

  2. sql azure 语法_使用Azure Data Studio从SQL Server数据创建图表

    sql azure 语法 In this article, we will explore charts in an Azure Data Studio using data stored in SQ ...

  3. sql azure 语法_使用Azure Data Studio开发SQL Server数据库

    sql azure 语法 In the previous article, Starting your journey with Azure Data Studio, we put the first ...

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

    azure上传代码 In the previous two articles, Starting your journey with Azure Data Studio and Developing ...

  5. sql azure 语法_方便SQL笔记本,用于在Azure Data Studio中进行故障排除

    sql azure 语法 This article prepares a handy SQL Notebook for DBAs. You can use this notebook to troub ...

  6. sql azure 语法_在Azure Data Studio中计划SQL笔记本

    sql azure 语法 SQL Notebooks are an interactive way of creating documents, executing T-SQL queries alo ...

  7. sql azure 语法_深入了解Azure Data Studio:扩展和Azure SQL DB开发

    sql azure 语法 In the previous articles listed below, we went through the Azure Data Studio tool, star ...

  8. sql azure 语法_在Azure Data Studio中学习用于SQL Notebook的Markdown语言

    sql azure 语法 Microsoft supports SQL Notebooks in Azure Data Studio. It is an exciting feature that a ...

  9. jenkins构建后脚本不执行_接口管理工具ApiPost-预(后)执行脚本常用方法集合

    本文主要讲解接口管理工具ApiPost的预执行脚本和后执行脚本里,常见的响应参数变量和常用方法集合. ApiPost简介: ApiPost是一个支持团队协作,并可直接生成文档的API调试.管理工具.它 ...

最新文章

  1. Python中numpy中tile和repeat用法和区别
  2. Linux正则表达式判断是否是数字示例
  3. Java IO/NIO教程
  4. python学习笔记(12)-python语法要求(缩进、标识符、变量)
  5. 最长公共子上升序列(信息学奥赛一本通-T1306)
  6. PyCharm设置背景颜色
  7. 这位曾指责乔布斯的怪老头,推出的AppleScript比Windows还要快!
  8. android自定义曲线控件,Android自定义折线图
  9. python读取excel在web中显示_在网页中预览excel表格文件
  10. BiliBili后台源码45MB全部泄露,中奖概率写在注释里,密码硬编码,看懂了你就欧气满满(提供下载)!
  11. 自定义firefox账户服务器出错,我在确认 Firefox 账户时出现了问题
  12. activiti7关联mysql_解决Springboot2.1.x配置Activiti7单独数据源问题
  13. hive 计算周几_HIVE 计算指定日期本周的第一天和最后一天
  14. ocr文字识别html,在线OCR 随时随地轻松搞定文字识别
  15. 线性表_顺序存储结构和链式存储结构的优缺点比较
  16. Gin源码之gin.Context结构体及其方法
  17. C语言——动态开辟内存空间的函数
  18. Java微信SDK方式进行网页授权
  19. Linux搭建tor网络环境
  20. PHP 26个英文字母递增

热门文章

  1. 使用 guard 的正确姿势
  2. 前端工程师的进阶之路
  3. Oracle 开窗函数--转
  4. LucasExlucas
  5. Linux环境CentOS6.9安装配置Elasticsearch6.2.2最全详细教程
  6. 数据库库存扣减乐观锁使用
  7. java知识点(记录用)
  8. 大量文本框非空判断,如何提高灵活性?
  9. 总结JavaScript中的继承
  10. Gentoo Framebuffer, Bootsplash Grubsplash 指南(修改版)