博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
比较和同步两个SQL Server数据库之间的图像
阅读量:2527 次
发布时间:2019-05-11

本文共 11853 字,大约阅读时间需要 39 分钟。

介绍 (Introduction)

Sometimes we lose data by mistake and we want to compare the data with older backups and synchronize the data.

有时我们会错误地丢失数据,我们想将数据与较早的备份进行比较并同步数据。

There are several ways to do this. For example, you can create T-SQL scripts with some outer joins. Another option is to use the command line.

有几种方法可以做到这一点。 例如,您可以使用一些外部联接来创建T-SQL脚本。 另一种选择是使用命令行。

The problem is that you need to create code or work for each single table. To solve this problem, there is a tool named that allows you to quickly compare all tables from a database with the other database, report the differences and synchronize the data.

问题是您需要为每个表创建代码或工作。 为解决此问题,有一个名为的工具,可让您快速将数据库中的所有表与另一个数据库进行比较,报告差异并同步数据。

要求 (Requirements)

  • ) 检查ApexSQL Data Diff支持哪些版本)
  • SQL Server Data Tools

    SQL Server数据工具
  • SQL Server Reporting Services

    SQL Server报告服务

入门 (Getting started)

In this tutorial, we will show 2 topics:

在本教程中,我们将显示2个主题:

  • How to compare and synchronize simple data from 2 databases

    如何比较和同步2个数据库中的简单数据
  • How to compare and synchronize and check a blog column (images)

    如何比较,同步和检查博客列(图像)

如何比较和同步2个数据库中的简单数据 (How to compare and synchronize simple data from 2 databases)

In the first example, we have a table named HumanResourses.Department2. We have 2 databases. Adventureworks2014 and Adventureworks2014backup. In the Adventureworks2014, there is an extra column inserted. We are going to compare all the tables from both databases and check the differences.

在第一个示例中,我们有一个名为HumanResourses.Department2的表。 我们有2个数据库。 Adventureworks2014和Adventureworks2014backup。 在Adventureworks2014中,插入了一个额外的列。 我们将比较两个数据库中的所有表并检查差异。

  1. On the Desktop or in the start menu, open the .

    在桌面或开始菜单中,打开 。

    Figure 1. ApexSQL Data Diff direct access
    图1. ApexSQL Data Diff直接访问
  2. You can access Recent projects or you can create a new one, by pressing the New button:

    您可以通过按“新建”按钮访问“最近的项目”或创建一个新的项目:

    Figure 2. Project management
    图2.项目管理
  3. Choose the connection properties and select the databases to compare. ApexSQL Data Diff compares all the tables in the database:

    选择连接属性,然后选择要比较的数据库。 ApexSQL Data Diff比较数据库中的所有表:

    Figure 3. The connection information
    图3.连接信息
  4. The software will load the metadata. It will check the names of the objects that match and finally the data will be compared:

    该软件将加载元数据。 它将检查匹配对象的名称,最后将对数据进行比较:

    Figure 4. Comparing data
    图4.比较数据
  5. In this example, the software detected a difference in the HumanResources.Department2 table. 1 row is missing in the Adventureworks2014backup database. That is why the number of rows is 17 in one database and 16 in another.

    在此示例中,软件在HumanResources.Department2表中检测到差异。 Adventureworks2014backup数据库中缺少1行。 这就是为什么在一个数据库中行数为17,而在另一个数据库中行数为16的原因。

    The report also says the data of the missing row:

    17, graphic design, Sales and Marketing, modified date.

    该报告还说缺少行的数据:

    17,平面设计,销售和市场推广,修改日期。

    Figure 5. The Results grid
    图5.结果网格
  6. If you check the Department2 table in the Adventureworks2014 database in SSMS, you will see the extra row:

    如果在SSMS中检查Adventureworks2014数据库中的Department2表,则会看到额外的行:

    Figure 6. The extra row in one of the databases
    图6.其中一个数据库中的额外行
  7. You can save the current project with the Save button:

    您可以使用“保存”按钮保存当前项目:

    Figure 7. The Save button
    图7.保存按钮
  8. The current project, with its set options, can be saved and stored in a file with .axdd extension. Saving a project can come in handy if the same settings and data sources are used often, so it can be loaded anytime, rather than setting up everything each time its needed:

    可以将当前项目及其设置的选项保存并存储在扩展名为.axdd的文件中。 如果经常使用相同的设置和数据源,则保存项目会很方便,因此可以随时加载,而不必在需要时进行所有设置:

    Figure 8. Exporting results
    图8.导出结果
  9. You can export the results to HTML, CSV and Excel format:

    您可以将结果导出为HTML,CSV和Excel格式:

    Figure 9. Exporting options
    图9.导出选项
  10. When you export, you will have the options to select what you want to see. You can choose which objects you want to export (different or equal), include the data comparison options, information on objects to export and include the rows that are different:

    导出时,您将可以选择要查看的内容。 您可以选择要导出的对象(不同或相等),包括数据比较选项,有关要导出的对象的信息,并包括不同的行:

    Figure 10. Report options
    图10.报告选项
  11. In the HTML report, you can see the source database, the destination database, the creation date of the report and the options. By default, it does not compare BLOB, ROWGUIDCOL columns and timestamp columns:

    在HTML报告中,您可以查看源数据库,目标数据库,报告的创建日期和选项。 默认情况下,它不比较BLOB,ROWGUIDCOL列和timestamp列:

    Figure 11. The ApexSQL Data Diff options
    图11. ApexSQL Data Diff选项
  12. You will also be able to see a report similar to the one in the Figure 5:

    您还将能够看到类似于图5中的报告:

    Figure 12. The HTML report
    图12. HTML报告
  13. Once you read and analyze the differences, you can synchronize your data with the Synchronize button:

    阅读并分析差异后,您可以使用“同步”按钮同步数据:

    Figure 13. The Synchronization wizard
    图13.同步向导
  14. You can change the synchronization direction in the first step of the Synchronization wizard. The destination database is the database that we want to update with the data of the source database:

    您可以在同步向导的第一步中更改同步方向。 目标数据库是我们要使用源数据库的数据更新的数据库:

    Figure 14. Synchronization direction
    图14.同步方向
  15. By clicking on the arrow, you can switch the source and destination:

    通过单击箭头,可以切换源和目标:

    Figure 15. Switching source and destination direction
    图15.切换源和目标方向

    In our example, we will use the Adventureworks2014 as the source and Adventureworks2014Backup as the destination.

    在我们的示例中,我们将使用Adventureworks2014作为源,并使用Adventureworks2014Backup作为目标。

  16. Once the synchronization direction is set, we can generate the script to synchronize the data in Internal editor or to perform direct synchronization:

    设置同步方向后,我们可以生成脚本以在内部编辑器中同步数据或执行直接同步:

    Figure 16. The options to process the synchronization.
    图16.处理同步的选项。
  17. The software will show the actions to be executed. In this example, a new row will be inserted in the destination database. Press the Create script button:

    该软件将显示要执行的动作。 在此示例中,新行将插入到目标数据库中。 按创建脚本按钮:

    Figure 17. The Actions to synchronize
    图17.同步的动作
  18. The script contains the T-SQL code that will synchronize the destination database. You can manually modify if you believe it is necessary or you can just check the code:

    该脚本包含将同步目标数据库的T-SQL代码。 如果您认为有必要,可以手动进行修改,也可以只检查以下代码:

    Figure 18. The synchronization code
    图18.同步代码
  19. To execute the code and apply it, run the Execute icon:

    要执行并应用代码,请运行“执行”图标:

    Figure 19. Executing the code
    图19.执行代码
  20. The final Wizard window will show if the execution was successful or not:

    最终的向导窗口将显示执行是否成功:

    Figure 20. Execution results
    图20.执行结果
  21. The software can optionally check again if there are differences once the script is applied:

    应用脚本后,该软件可以选择再次检查是否存在差异:

    Figure 21. New comparison
    图21.新的比较
  22. You can also check in the Adventureworks2014Backup database that the new row was added successfully.

    您也可以在Adventureworks2014Backup数据库中检查是否成功添加了新行。

    Figure 22. The new row added
    图22.添加的新行

As you can see, synchronizing 2 databases is very easy. This tool synchronizes data, if you want to compare objects, you can use the .

如您所见,同步2个数据库非常容易。 该工具同步数据,如果要比较对象,则可以使用 。

Many people ask if the software can compare BLOB columns like pictures. The following example will show how to synchronize images in SQL Server with .

许多人问该软件是否可以比较BLOB列(如图片)。 以下示例将显示如何在SQL Server中将图像与同步。

如何比较,同步和检查BLOB列(图像)。 (How to compare and synchronize and check BLOB columns (images).)

The next example will show how to compare and synchronize images stored in a SQL table. We will also check the image updated using the Reporting Services.

下一个示例将显示如何比较和同步存储在SQL表中的图像。 我们还将检查使用Reporting Services更新的图像。

  1. The first step is to go to the ApexSQL Data Diff application and uncheck the Ignore BLOB columns option under the Options tab in the New project window. This will allow the comparison of BLOB columns (which are ignored by default):

    第一步是转到ApexSQL Data Diff应用程序,然后取消选中“新建项目”窗口中“选项”选项卡下的“ 忽略BLOB列”选项。 这将允许比较BLOB列(默认情况下将被忽略):

    Figure 23. Options tab
    图23.选项选项卡
  2. In this example, we are going to create 1 table in each database named myimage. This table will store images:

    在此示例中,我们将在每个名为myimage的数据库中创建1个表。 该表将存储图像:

     CREATE TABLE myimage(imagecolumn varbinary(max)) 
  3. We will store two different images on the tables just create. The first image is smileyface.jpg which will be stored in the Adventureworks2014 database.

    我们将在刚创建的表上存储两个不同的图像。 第一张图片是smileyface.jpg,它将存储在Adventureworks2014数据库中。

    Figure 24. The smiley picture
    图24.笑脸图片
  4. The second picture named sadface.jpg will be stored in the Adventureworks2014Backup database.

    第二张名为sadface.jpg的图片将存储在Adventureworks2014Backup数据库中。

    Figure 25. The sad face
    图25.悲伤的脸
  5. In the Adventureworks2014, insert the smileyface.jpg.

    在Adventureworks2014中,插入smileyface.jpg。

     INSERT INTO myimage(imagecolumn) SELECT * FROM OPENROWSET(BULK N'e:\smileyface.jpg', SINGLE_BLOB) as T1 
  6. In the Adventureworks2014Backup, insert the sadface.jpg.

    在Adventureworks2014Backup中,插入sadface.jpg。

     INSERT INTO myimage(imagecolumn) SELECT * FROM OPENROWSET(BULK N'e:\sadface.jpg', SINGLE_BLOB) as T1 
  7. The procedure to compare is the same as in the first example from step 1 until 5. As you can see, the software detects that the images are different:

    比较步骤与步骤1到步骤5的第一个示例相同。如您所见,该软件检测到图像不同:

    Figure 26. The data comparison of BLOB columns (images)
    图26. BLOB列的数据比较(图像)
  8. Select the synchronization direction so that the AdventureWorks2014 database is set as the source and the AdventureWorks2014Backup as the destination. This synchronization will change the sad face to a smiley face (the typical happy end):

    选择同步方向,以便将AdventureWorks2014数据库设置为源,将AdventureWorks2014Backup设置为目标。 这种同步会将悲伤的脸变成笑脸(典型的幸福结局):

    Figure 27. The Synchronization of source and destination
    图27.源和目标的同步
  9. If you synchronize and check the code as we did from steps 13 to 18 in the example one, we will be able to see the following code:

    如果像示例一中的步骤13到18那样同步并检查代码,我们将能够看到以下代码:

    Figure 28. The synchronization code
    图28.同步代码
  10. To visualize the image you can use SQL Server Reporting Services.

    要形象化图像,可以使用SQL Server Reporting Services。

  11. In the SQL Services Data Tools, go to File>New Project and select the Report Server Wizard:

    在SQL Services数据工具中,转到“文件”>“新建项目”,然后选择“报表服务器向导”:

    Figure 29. Creating a new project
    图29.创建一个新项目
  12. Press the edit button to select the SQL Server name, and the database. In this example, we want to see if the Adventureworks2014Backup database image stored was changed successfully using the ApexSQL Data Diff software:

    按下编辑按钮以选择SQL Server名称和数据库。 在此示例中,我们想查看使用ApexSQL Data Diff软件是否成功更改了存储的Adventureworks2014Backup数据库映像:

    Figure 30. The SQL Server Credential
    图30. SQL Server凭证
  13. Specify the query to the table with the image:

    将查询指定到带有图像的表:

    Figure 31. The Query used in the report
    图31.报告中使用的查询
  14. I will not explain each window wizard. If you have problems to generate a report, you can read . Once the report is done, drag and drop the image from the Toolbox to the report:

    我不会解释每个窗口向导。 如果您在生成报告时遇到问题,可以阅读 。 完成报告后,将图像从“工具箱”拖放到报告中:

    Figure 32. Adding images to the report
    图32.将图像添加到报告中
  15. In the image properties, in the select image source, select Database. In the use this field: select the column with the image. The MIME type will allow selecting the extension of the image

    在图像属性中的选择图像源中,选择“数据库”。 在“使用此字段”中:选择包含图像的列。 MIME类型将允许选择图像的扩展名

    Figure 33. Image properties
    图33.图像属性
  16. Select the image and go to properties. In the sizing, select AutoSize. This will help to show the image in an appropriate size:

    选择图像并转到属性。 在调整大小中,选择“自动调整大小”。 这将有助于以适当的大小显示图像:

    Figure 34. Image properties
    图34.图像属性
  17. Select the previous tab. As you can see, the sad face was changed to a smiley face using the . As promised, we had a happy conclusion.

    选择上一个标签。 如您所见,使用将悲伤的脸变成了笑脸。 如所承诺的,我们有一个愉快的结论。

结论 (Conclusions)

In this article, we learned how to compare the data between two databases. We also learned how to compare images stored in SQL Server. As you can see, the comparison and synchronization is a straightforward process even when we work with BLOB columns (images), ROWGUIDCOL, or other special columns. If you need to compare SQL objects, use .

在本文中,我们学习了如何比较两个数据库之间的数据。 我们还学习了如何比较存储在SQL Server中的图像。 如您所见,即使我们使用BLOB列(图像),ROWGUIDCOL或其他特殊列,比较和同步也是一个简单的过程。 如果需要比较SQL对象,请使用 。

翻译自:

转载地址:http://isiwd.baihongyu.com/

你可能感兴趣的文章
Selenium API(一)
查看>>
今天捡起来python
查看>>
解决mysql提权user.MYD hash无法查看
查看>>
Vector 容器简单介绍
查看>>
elasticsearch报错:None of the configured nodes are available: []
查看>>
版本控制工具git
查看>>
将 ExpressRoute 线路从经典部署模型转移到 Resource Manager 部署模型
查看>>
[Pytorch]深度模型的显存计算以及优化
查看>>
ASP.NET将word文档转换成pdf的代码
查看>>
dubbo 线程池
查看>>
贪婪和非贪婪
查看>>
java.lang.Exception: Socket bind failed 服务器端口冲突-->修改端口
查看>>
linux系统usb挂载
查看>>
拓扑排序应用(leetcode 310 python)
查看>>
[Windows Phone] 为应用添加后台计划任务 – Scheduled Task Agent
查看>>
decode、sign、case在统计中的用法:
查看>>
解决如何让AsyncTask终止操作
查看>>
C#中的关键字
查看>>
一条经典SQL语句优化实例
查看>>
Ruby_day[1]控制流(if, unless)
查看>>