求解SQL server2008 比较两个数据库差异,但是我要数据库表里有数据的才比较

时间:2023-01-20 08:52:19
我做了两个数据库差异对比,我想查两个数据库里有数据的表才去对比差异。负上代码:
USE master
-----------------
GO
--视图、存储过程比较
/*--调用示例 
exec p_compdb 'DBNAME1','DBNAME2' 
exec p_compdb 'DBNAME2','DBNAME3' 
--*/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[p_compdb]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[p_compdb]
GO
CREATE PROC p_compdb 
@db1 sysname, --第一个库 
@db2 sysname --第二个库 
as
exec(' 
select 类型=case isnull(a.xtype,b.xtype) when ''V'' then ''视图'' when ''TF'' then ''表值函数''  when ''IF'' then ''表值函数'' when ''FN'' then ''标量值函数''  when ''TR'' then ''触发器'' else ''存储过程'' end 
,匹配情况=case 
when a.name is null then ''库 ['+@db1+'] 中无'' 
when b.name is null then ''库 ['+@db2+'] 中无'' 
else ''结构不同'' end 
,对象名称=isnull(a.name,b.name),a.text as atext, b.text as btext
from( 
select a.name,a.xtype,b.colid,b.text 
from ['+@db1+']..sysobjects a,['+@db1+']..syscomments b 
where a.id=b.id and a.xtype in(''V'',''P'',''TF'',''IF'',''FN'',''TR'') and a.status>=0 
)a full join( 
select a.name,a.xtype,b.colid,b.text 
from ['+@db2+']..sysobjects a,['+@db2+']..syscomments b 
where a.id=b.id and a.xtype in(''V'',''P'',''TF'',''IF'',''FN'',''TR'') and a.status>=0 
)b on a.name=b.name and a.xtype=b.xtype and a.colid=b.colid 
where a.name is null 
or b.name is null 
or isnull(a.text,'''') <>isnull(b.text,'''') 
--group by a.name,b.name,a.xtype,b.xtype 
order by 类型,匹配情况,对象名称') 
--sys.objects.type 类型枚举值如下:
--AF  =   聚合函数   (CLR) 
--C   =   CHECK   约束 
--D   =   DEFAULT(约束或独立) 
--F   =   FOREIGN   KEY   约束 
--PK   =   PRIMARY   KEY   约束 
--P   =   SQL   存储过程 
--PC   =   程序集   (CLR)   存储过程 
--FN   =   SQL   标量函数 
--FS   =   程序集   (CLR)   标量函数 
--FT   =   程序集   (CLR)   表值函数 
--R   =   规则(旧式,独立) 
--RF   =   复制筛选过程 
--SN   =   同义词 
--SQ   =   服务队列 
--TA   =   程序集   (CLR)   DML   触发器 
--TR   =   SQL   DML   触发器 
--IF   =   SQL   内联表值函数 
--TF   =   SQL   表值函数 
--U   =   表(用户定义类型) 
--UQ   =   UNIQUE   约束 
--V   =   视图 
--X   =   扩展存储过程 
--IT   =   内部表
GO

--数据表结构比较
/*--调用示例 
exec p_comparestructure 'DBNAME1','DBNAME2' 
exec p_comparestructure 'DBNAME2','DBNAME3' 
--*/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[p_comparestructure]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[p_comparestructure]
GO
CREATE PROC p_comparestructure 
@dbname1 varchar(250),--要比较的数据库名1 
@dbname2 varchar(250) --要比较的数据库名2 
as
create table #tb1(表名1 varchar(250),字段名 varchar(250),序号 int,标识 bit,主键 bit,类型 varchar(250), 
占用字节数 int,长度 int,小数位数 int,允许空 bit,默认值 sql_variant,字段说明 sql_variant) 
  
create table #tb2(表名2 varchar(250),字段名 varchar(250),序号 int,标识 bit,主键 bit,类型 varchar(250), 
占用字节数 int,长度 int,小数位数 int,允许空 bit,默认值 sql_variant,字段说明 sql_variant) 
  
  

--得到数据库1的结构 
exec('insert into #tb1 SELECT 
表名=d.name,字段名=a.name,序号=a.colid, 
标识=case when a.status=0x80 then 1 else 0 end, 
主键=case when exists(SELECT 1 FROM '+@dbname1+'..sysobjects  where xtype=''PK''  and parent_obj=a.id and name in ( 
SELECT name FROM '+@dbname1+'..sysindexes WHERE indid in( 
SELECT indid FROM '+@dbname1+'..sysindexkeys WHERE id = a.id AND colid=a.colid 
))) then 1 else 0 end, 
类型=b.name,占用字节数=a.length,长度=a.prec,小数位数=a.scale,允许空=a.isnullable, 
默认值=isnull(e.text,''''),字段说明=isnull(g.[value],'''') 
FROM '+@dbname1+'..syscolumns a 
left join '+@dbname1+'..systypes b on a.xtype=b.xusertype 
inner join '+@dbname1+'..sysobjects d on a.id=d.id  and d.xtype=''U'' and  d.name <>''dtproperties'' 
left join '+@dbname1+'..syscomments e on a.cdefault=e.id 
left join sys.extended_properties g 
ON 
a.ID=g.major_id AND a.COLID=g.minor_id
order by a.id,a.colorder') 
  
--得到数据库2的结构 
exec('insert into #tb2 SELECT 
表名=d.name,字段名=a.name,序号=a.colid, 
标识=case when a.status=0x80 then 1 else 0 end, 
主键=case when exists(SELECT 1 FROM '+@dbname2+'..sysobjects  where xtype=''PK'' and parent_obj=a.id and name in ( 
SELECT name FROM '+@dbname2+'..sysindexes WHERE indid in( 
SELECT indid FROM '+@dbname2+'..sysindexkeys WHERE id = a.id AND colid=a.colid 
))) then 1 else 0 end, 
类型=b.name,占用字节数=a.length,长度=a.prec,小数位数=a.scale,允许空=a.isnullable, 
默认值=isnull(e.text,''''),字段说明=isnull(g.[value],'''') 
FROM '+@dbname2+'..syscolumns a 
left join '+@dbname2+'..systypes b on a.xtype=b.xusertype 
inner join '+@dbname2+'..sysobjects d on a.id=d.id  and d.xtype=''U'' and  d.name <>''dtproperties'' 
left join '+@dbname2+'..syscomments e on a.cdefault=e.id 
left join sys.extended_properties g 
ON 
a.ID=g.major_id AND a.COLID=g.minor_id 
order by a.id,a.colorder') 
--and not exists(select 1 from #tb2 where 表名2=a.表名1) 

select 比较结果=case when a.表名1 is null and b.序号=1 then '库1【'+@dbname1+'】缺少表:'+b.表名2 
when b.表名2 is null and a.序号=1 then '库2【'+@dbname2+'】缺少表:'+a.表名1 
when a.字段名 is null and exists(select 1 from #tb1 where 表名1=b.表名2) then '库1【'+@dbname1+'】['+b.表名2+'] 缺少字段:'+b.字段名 
when b.字段名 is null and exists(select 1 from #tb2 where 表名2=a.表名1) then '库2 【'+@dbname2+'】['+a.表名1+'] 缺少字段:'+a.字段名 
when a.标识 <>b.标识 then '标识不同'
when a.主键 <>b.主键 then '主键设置不同'
when a.类型 <>b.类型 then '字段类型不同'
when a.占用字节数 <>b.占用字节数 then '占用字节数'
when a.长度 <>b.长度 then '长度不同'
when a.小数位数 <>b.小数位数 then '小数位数不同'
when a.允许空 <>b.允许空 then '是否允许空不同'
when a.默认值 <>b.默认值 then '默认值不同'
when a.字段说明 <>b.字段说明 then '字段说明不同'
else '' end, 

from #tb1 a 
full join #tb2 b on a.表名1=b.表名2 and a.字段名=b.字段名 
where a.表名1 is null or a.字段名 is null or b.表名2 is null or b.字段名 is null
or a.标识 <>b.标识 or a.主键 <>b.主键 or a.类型 <>b.类型 
or a.占用字节数 <>b.占用字节数 or a.长度 <>b.长度 or a.小数位数 <>b.小数位数 
or a.允许空 <>b.允许空 or a.默认值 <>b.默认值 or a.字段说明 <>b.字段说明 
order by isnull(a.表名1,b.表名2),isnull(a.序号,b.序号)--isnull(a.字段名,b.字段名) 

GO
 
这是我查出单个数据库有数据的代码:

declare @spaceused table (
name varchar(128),
rows char(20), 
reserced varchar(18),
date varchar(18),
index_size varchar(18),
unuesd varchar(18)
);
declare @cursor cursor;
declare @name varchar(128);

set @cursor = cursor for
   select
    name
   from
    sysobjects
   where
    xtype = 'u'
open @cursor
fetch next from @cursor into @name
while @@fetch_status = 0
begin 
  insert into @spaceused
  EXEC sp_spaceused  @name
  fetch next from @cursor into @name
end


select name '名称',rows '数据行',reserced '保留',date '数据',index_size 'index_size',unuesd '未使用' FROM @SPACEUSED where rows>'0' order by name



大神们求解

6 个解决方案

#1


好长的代码啊 求解SQL server2008 比较两个数据库差异,但是我要数据库表里有数据的才比较

#2


求解SQL server2008 比较两个数据库差异,但是我要数据库表里有数据的才比较求解

#3


求解SQL server2008 比较两个数据库差异,但是我要数据库表里有数据的才比较我们公司有比较差异的软件

#4


引用 3楼saiwen8888 的回复:
求解SQL server2008 比较两个数据库差异,但是我要数据库表里有数据的才比较我们公司有比较差异的软件
求解SQL server2008 比较两个数据库差异,但是我要数据库表里有数据的才比较求解SQL server2008 比较两个数据库差异,但是我要数据库表里有数据的才比较

#6


好长的代码,不过分还挺高的~

#1


好长的代码啊 求解SQL server2008 比较两个数据库差异,但是我要数据库表里有数据的才比较

#2


求解SQL server2008 比较两个数据库差异,但是我要数据库表里有数据的才比较求解

#3


求解SQL server2008 比较两个数据库差异,但是我要数据库表里有数据的才比较我们公司有比较差异的软件

#4


引用 3楼saiwen8888 的回复:
求解SQL server2008 比较两个数据库差异,但是我要数据库表里有数据的才比较我们公司有比较差异的软件
求解SQL server2008 比较两个数据库差异,但是我要数据库表里有数据的才比较求解SQL server2008 比较两个数据库差异,但是我要数据库表里有数据的才比较

#5


#6


好长的代码,不过分还挺高的~