本文共 11853 字,大约阅读时间需要 39 分钟。
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.
问题是您需要为每个表创建代码或工作。 为解决此问题,有一个名为的工具,可让您快速将数据库中的所有表与另一个数据库进行比较,报告差异并同步数据。
In this tutorial, we will show 2 topics:
在本教程中,我们将显示2个主题:
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中,插入了一个额外的列。 我们将比较两个数据库中的所有表并检查差异。
On the Desktop or in the start menu, open the .
在桌面或开始菜单中,打开 。
You can access Recent projects or you can create a new one, by pressing the New button:
您可以通过按“新建”按钮访问“最近的项目”或创建一个新的项目:
Choose the connection properties and select the databases to compare. ApexSQL Data Diff compares all the tables in the database:
选择连接属性,然后选择要比较的数据库。 ApexSQL Data Diff比较数据库中的所有表:
The software will load the metadata. It will check the names of the objects that match and finally the data will be compared:
该软件将加载元数据。 它将检查匹配对象的名称,最后将对数据进行比较:
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,平面设计,销售和市场推广,修改日期。
If you check the Department2 table in the Adventureworks2014 database in SSMS, you will see the extra row:
如果在SSMS中检查Adventureworks2014数据库中的Department2表,则会看到额外的行:
You can save the current project with the Save button:
您可以使用“保存”按钮保存当前项目:
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的文件中。 如果经常使用相同的设置和数据源,则保存项目会很方便,因此可以随时加载,而不必在需要时进行所有设置:
You can export the results to HTML, CSV and Excel format:
您可以将结果导出为HTML,CSV和Excel格式:
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:
导出时,您将可以选择要查看的内容。 您可以选择要导出的对象(不同或相等),包括数据比较选项,有关要导出的对象的信息,并包括不同的行:
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列:
You will also be able to see a report similar to the one in the Figure 5:
您还将能够看到类似于图5中的报告:
Once you read and analyze the differences, you can synchronize your data with the Synchronize button:
阅读并分析差异后,您可以使用“同步”按钮同步数据:
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:
您可以在同步向导的第一步中更改同步方向。 目标数据库是我们要使用源数据库的数据更新的数据库:
By clicking on the arrow, you can switch the source and destination:
通过单击箭头,可以切换源和目标:
In our example, we will use the Adventureworks2014 as the source and Adventureworks2014Backup as the destination.
在我们的示例中,我们将使用Adventureworks2014作为源,并使用Adventureworks2014Backup作为目标。
Once the synchronization direction is set, we can generate the script to synchronize the data in Internal editor or to perform direct synchronization:
设置同步方向后,我们可以生成脚本以在内部编辑器中同步数据或执行直接同步:
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:
该软件将显示要执行的动作。 在此示例中,新行将插入到目标数据库中。 按创建脚本按钮:
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代码。 如果您认为有必要,可以手动进行修改,也可以只检查以下代码:
To execute the code and apply it, run the Execute icon:
要执行并应用代码,请运行“执行”图标:
The final Wizard window will show if the execution was successful or not:
最终的向导窗口将显示执行是否成功:
The software can optionally check again if there are differences once the script is applied:
应用脚本后,该软件可以选择再次检查是否存在差异:
You can also check in the Adventureworks2014Backup database that the new row was added successfully.
您也可以在Adventureworks2014Backup数据库中检查是否成功添加了新行。
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中将图像与同步。
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更新的图像。
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列(默认情况下将被忽略):
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))
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数据库中。
The second picture named sadface.jpg will be stored in the Adventureworks2014Backup database.
第二张名为sadface.jpg的图片将存储在Adventureworks2014Backup数据库中。
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
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
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的第一个示例相同。如您所见,该软件检测到图像不同:
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设置为目标。 这种同步会将悲伤的脸变成笑脸(典型的幸福结局):
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那样同步并检查代码,我们将能够看到以下代码:
To visualize the image you can use SQL Server Reporting Services.
要形象化图像,可以使用SQL Server Reporting Services。
In the SQL Services Data Tools, go to File>New Project and select the Report Server Wizard:
在SQL Services数据工具中,转到“文件”>“新建项目”,然后选择“报表服务器向导”:
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数据库映像:
Specify the query to the table with the image:
将查询指定到带有图像的表:
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:
我不会解释每个窗口向导。 如果您在生成报告时遇到问题,可以阅读 。 完成报告后,将图像从“工具箱”拖放到报告中:
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类型将允许选择图像的扩展名
Select the image and go to properties. In the sizing, select AutoSize. This will help to show the image in an appropriate size:
选择图像并转到属性。 在调整大小中,选择“自动调整大小”。 这将有助于以适当的大小显示图像:
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.
选择上一个标签。 如您所见,使用将悲伤的脸变成了笑脸。 如所承诺的,我们有一个愉快的结论。
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/