简体   繁体   English

SQL Server 2005数据库比较

[英]SQL Server 2005 database comparison

I am working on a project of Data Centralization. 我正在从事数据集中化项目。 I have to transfer data from two databases into a third newly created master database.The existing databases hold the same type of data, the tables are the same etc. But before that, I need to check whether the master tables from the existing two databases are the same ie the column names, number etc are same of the two same tables in both the databases. 我必须将数据从两个数据库转移到第三个新创建的主数据库中。现有数据库保存相同类型的数据,表是相同的,等等。但是在此之前,我需要检查是否存在于现有两个数据库中的主表相同,即两个数据库中两个相同表的列名,编号等相同。 For eg Both the databases have a table called SROMaster. 例如,两个数据库都有一个名为SROMaster的表。 I need to check if SROMaster from db1 will hold the same columns as SROMaster from db2. 我需要检查db1中的SROMaster是否与db2中的SROMaster保持相同的列。 Can someone help me? 有人能帮我吗? Thanks! 谢谢!

To check a single table, you could just issue the convert table to Create script command on both databases and compare the results by hand. 要检查单个表,您只需在两个数据库上向转换表发出“创建脚本”命令,然后手动比较结果。

You may also have to use some judgment re: what is compatible, ie, are the columns in the same order? 您可能还必须使用一些判断:与什么兼容,即列的顺序相同吗? Does that matter to you based on how you plan on converted it. 这对您而言是否重要,取决于您计划如何对其进行转换。

If you have a large number of table to compare, you don't want to do this by hand. 如果您有大量要比较的表,则不希望手动进行此操作。 There are tools for this, or you can roll you own by having proc that run through the system catalog. 有用于此目的的工具,或者您可以通过在系统目录中运行proc来推出自己的工具。

Given that you are asking this question, I suspect you are better off looking for an existing tool to do the heavy lifting of table comparison. 鉴于您是在问这个问题,我怀疑您最好选择一个现有的工具来进行繁重的表比较。 This is not considered a stack overflow question though. 但是,这不被视为堆栈溢出问题。

Not sure what platform/language you're using - but in any case, to test if a given table exists, you can use this T-SQL to do the job: 不确定使用的是哪种平台/语言-但是在任何情况下,要测试给定表是否存在,都可以使用以下T-SQL来完成工作:

SELECT t.*
FROM sys.tables t
WHERE t.Name = 'SROMaster'

That will return a row with all the info about the table - if it exists - otherwise you can nothing back. 这将返回包含有关表的所有信息的行(如果存在的话),否则您将一无所有。

Once you have the table, you can check what columns the table has by using: 拥有表格后,可以使用以下方法检查表格具有的列:

SELECT c.*
FROM sys.columns c
INNER JOIN sys.tables t ON c.object_id = t.object_id
WHERE t.Name = 'SROMaster'
ORDER BY c.column_id

You'll get back a data set of rows with information about the columns - compare those two lists you're getting to see if you have the same columns in both table's SROMaster table. 您将获得包含有关列信息的行数据集-比较这两个列表,以查看两个表的SROMaster表中是否具有相同的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM