介绍 (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 tablediff command line.

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

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

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

要求 (Requirements)

  • here) 在此处检查ApexSQL Data Diff支持哪些版本)
  • ApexSQL Data Diff ApexSQL数据差异
  • 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 ApexSQL Data Diff.

    在桌面或开始菜单中,打开ApexSQL数据差异 。

    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 ApexSQL Diff.

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

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 ApexSQL Data Diff.

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

如何比较,同步和检查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 our article about reporting services. 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 ApexSQL Data Diff. As promised, we had a happy conclusion.

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

结论 (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 ApexSQL Diff.

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

翻译自: https://www.sqlshack.com/compare-and-synchronize-images-between-two-sql-server-databases/

比较和同步两个SQL Server数据库之间的图像相关推荐

  1. [转载]在SQL Server数据库之间进行数据导入导出,OPENDATASOURCE

    需要在c盘下先建立一个data.txt文件,然后在文件的第一行写上你要导出的列,不如说要导出id和name这两列,就在第一行写上 id,name 然后保存,使用下列SQL就可以了,你如果要保持原有的I ...

  2. 在SQL Server数据库之间进行数据导入导出

    来源:http://kb.cnblogs.com/page/94464/ 在SQL Server数据库之间进行数据导入导出 (1).使用SELECT INTO导出数据 在SQL Server中使用最广 ...

  3. Sql Server数据库之间如何进行异地远程连接

    最近在做软件工程课程设计,因为是小组作业,懒得每个队友建立一个数据库,于是就想说只有一个人建数据库,其余人连上,这样就算一个人改动了其中的数据,其他人数据库的内容也是可以同步的. 查阅了很多资料,现在 ...

  4. 如何下载和安装SQL Server数据库实验助手(DEA)

    介绍 (Introduction) The Database Experimentation Assistant (DEA) is a new Experimental solution provid ...

  5. SQL Server数据库导入导出数据方式比较

    在我们建立一个数据库时,并且想将分散在各处的不同类型的数据库分类汇总在这个新建的数据库中时,尤其是在进行数据检验.净化和转换时,将会面临很大的挑战.幸好SQL Server为我们提供了强大.丰富的数据 ...

  6. 数据库应用程序开发基础篇—— .NET中SQL Server数据库的操作C#篇之一

    数据库应用程序开发基础篇-- .NET中SQL Server数据库的操作C#篇之一 写在前面:前面介绍了数据库系统的基本概念,SQl语句基本使用方法,接下来通过学习具体语言和具体数据库结合的应用开发来 ...

  7. SQL Server数据库重命名、数据导出等若干问题

    第一个问题是重命名数据库问题:在企业管理器中是无法直接对数据库重命名的,只能在查询分析器中操作   create proc killspid (@dbname varchar(20))        ...

  8. SQL Server数据库同步问题分享[未完,待续](一)

    SQL Server数据库同步问题分享[未完,待续](一) SQL Server数据库同步问题分享(二)---操作步骤[未完,待续] SQL Server数据库同步问题分享(三)---创建订阅 合并发 ...

  9. 如何对两个大型SQL Server数据库中的数据进行快速估计比较,以查看它们是否相等

    Bringing impactful analysis into a data always comes with challenges. In many cases, we rely on auto ...

最新文章

  1. C语言中的位运算和逻辑运算
  2. 结束下面sql块_如何执行超过100M的SQL脚本?
  3. hashMap与hashTable区别
  4. OpenCV4.4.0+VS2017 环境配置
  5. 无向图双连通分量BCC(全网最好理解)
  6. ios开发之 -- 自动轮播图创建
  7. .NET WinForm程序中给DataGridView表头添加下拉列表实现数据过滤
  8. java i线程安全吗_Java中 i++ 是线程安全的么?为什么?
  9. 【NOIP2016提高A组模拟10.15】算循环
  10. java中aop和aoc的区别_你喝到的波尔多AOC、AOP红酒是真的吗?
  11. java返回首页提示空值_是否有使用Optional初始化和返回可为空字段的值的优雅方法 - java...
  12. C语言爱心代码,C语言爱心代码合集(附源码)
  13. 一文看懂:销售数据分析怎么做?
  14. unity 3d 仿真_在Unity3D中构建3D仿真入门,第1部分
  15. 现在的男生为什么不主动追求女生了
  16. JAVA看云判断天气_如何看云识天气?
  17. createrepo的用法
  18. 背完这10个经典面试答案,80%的面试都能喜提offer
  19. Css margin和pandding的上下左右
  20. Hyperledger Fabric网络环境手动配置及其链码自动化部署

热门文章

  1. orderquery.php,multiOrderQuery.php
  2. php中header用法
  3. 数据库库存扣减乐观锁使用
  4. automaticallyAdjustsScrollViewInsets 标签栏不正常显示
  5. 委托和事件[delegate and event]_C#
  6. Spoon新建repository的时候
  7. 关于html中css无法作用的问题
  8. opencv新手注意
  9. Protocol Buffers proto语言语法说明
  10. 小白必须要会的Github操作 确定不进来看看?