create Table #t1
(
ID Int Identity(1,1) Not Null Primary Key,
tablename nvarchar(50) NULL,
columnName nvarchar(50) NULL,
columnIndex int null,
columnType nvarchar(50) NULL
)
use 数据库1
insert into #t1
create Table #t2
(
ID Int Identity(1,1) Not Null Primary Key,
tablename nvarchar(50) NULL,
columnName nvarchar(50) NULL,
columnIndex int null,
columnType nvarchar(50) NULL
)
//开始比较
use 数据库2
insert into #t2
SELECT
SO.name as '表名',
SC.name as '表列名',
SC.colid as '索引',
ST.name as '类型'
FROM
sysobjects SO, -- 对象表
syscolumns SC, -- 列名表
systypes ST -- 数据类型表
WHERE
SO.id = SC.id
AND SO.xtype = 'U'-- 类型U表示表,V表示视图
AND SO.status >= 0 --加一个条件:SO.status >= 0,否则会将系统的临时表显示出来
AND SC.xtype = ST.xusertype
ORDER BY
SO.name, SC.colorder
go
//查询出 在t1 里有, t2 里没有的字段,查询列出来。
select * from
(
select tablename,columnName,columnType from #t1 where tablename like '%EMS_%'
EXCEPT
select tablename,columnName,columnType from #t2 where tablename like '%EMS_%'
) as c
order by tablename
比较两个数据库中表和字段的差异
-- 比较两个数据库中表的差异
-- u表,p存储过程,v视图
-- INTFSIMSNEW新库,INTFSIMS旧库
SELECT NTABLE = A.NAME, OTABLE = B.NAME
FROM INTFSIMSNEW..SYSOBJECTS A
LEFT JOIN INTFSIMS..SYSOBJECTS B
ON A.NAME = B.NAME
WHERE ISNULL(B.NAME, '') = ''
AND A.XTYPE = 'U'
UNION ALL
SELECT NTABLE = B.NAME, OTABLE = A.NAME
FROM INTFSIMS..SYSOBJECTS A
LEFT JOIN INTFSIMSNEW..SYSOBJECTS B
ON A.NAME = B.NAME
WHERE ISNULL(B.NAME, '') = ''
AND A.XTYPE = 'U'
ORDER BY 1, 2
-- 比较两个数据库中每个表字段的差异
SELECT
表名A = CASE WHEN ISNULL(A.TABLENAME, '') <>'' THEN A.TABLENAME ELSE B.TABLENAME END,
字段名A = A.FIELDNAME,
字段名B = B.FIELDNAME,
顺序= A.FIELDSNO,
说明= CASE WHEN A.FIELDTYPE <>B.FIELDTYPE THEN '类型: ' + A.FIELDTYPE + '-->' + B.FIELDTYPE
WHEN A.FIELDSNO <>B.FIELDSNO THEN '顺序: ' + str(A.FIELDSNO) + '-->' + str(B.FIELDSNO)
WHEN A.LENGTH <>B.LENGTH THEN '长度: ' + str(A.LENGTH) + '-->' + str(B.LENGTH)
WHEN A.LENSEC <>B.LENSEC THEN '小数位: ' + str(A.LENSEC) + '-->' + str(B.LENSEC)
WHEN A.ALLOWNULL <>B.ALLOWNULL THEN '允许空值: ' + str(A.ALLOWNULL) + '-->' + str(B.ALLOWNULL)
END
FROM (SELECT
TABLENAME = B.NAME,
FIELDNAME = A.NAME,
FIELDSNO = A.COLID,
FIELDTYPE = C.NAME,
LENGTH = A.LENGTH,
LENSEC = A.XSCALE,
ALLOWNULL = A.ISNULLABLE
FROM INTFSIMSNEW..SYSCOLUMNS A
LEFT JOIN INTFSIMSNEW..SYSOBJECTS B
ON A.ID = B.ID
LEFT JOIN INTFSIMSNEW..SYSTYPES C
ON A.XUSERTYPE = C.XUSERTYPE
WHERE B.XTYPE = 'U') A
FULL JOIN (SELECT
TABLENAME = B.NAME,
FIELDNAME = A.NAME,
FIELDSNO = A.COLID,
FIELDTYPE = C.NAME,
LENGTH = A.LENGTH,
LENSEC = A.XSCALE,
ALLOWNULL = A.ISNULLABLE
FROM INTFSIMS..SYSCOLUMNS A
LEFT JOIN INTFSIMS..SYSOBJECTS B
ON A.ID = B.ID
LEFT JOIN INTFSIMS..SYSTYPES C
ON A.XUSERTYPE = C.XUSERTYPE
WHERE B.XTYPE = 'U') B
ON A.TABLENAME = B.TABLENAME
AND A.FIELDNAME = B.FIELDNAME
WHERE ISNULL(A.TABLENAME, '') = ''
OR ISNULL(B.TABLENAME, '') = ''
OR A.FIELDTYPE <>B.FIELDTYPE
OR A.FIELDSNO <>B.FIELDSNO
OR A.LENGTH <>B.LENGTH
OR A.LENSEC <>B.LENSEC
OR A.ALLOWNULL <>B.ALLOWNULL
ORDER by 1, 4
不知道你说的表结构还是表记录的不同,表结构比较很简单,从数据字典里读出每个表的数据(包含字段、类型、索引等等)形成文件,然后把两个数据库结构文件用对比工具就可以搞定,当然现在有很多的现行工具能够将所有表结构形成文本文件(ORACLE如PL/SQL工具);记录的比较就需要你逐表写SQL语句比较了或者将表里的数据按照一定规则导出成文件比较如果两个数据库可以建立dblink,那么通过dblink就可以像在一个数据库一样去对比。如果不能建立dblink,那么就只能导出两行表,通过spool导出就可以。然后通过系统级别的命令去比对。我记得linux是有比较两个文件不同行的功能的。
如果认为两个都导出比较麻烦,那就导出一个,在导入另外一个库(注意变换名字,否则表明重复就麻烦了),然后再对比。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)