SQL数据库比较存储过程

-- =============================================
-- Author:大乌龟的
-- Description:    <比较两个数据库的表结构差异并自动生成修改和插入表字段脚本>
--exec p_comparestructure 'ZhongYDBDevelop','ZhongYDBIssued'
-- =============================================
ALTER proc [dbo].[p_comparestructure]
@dbname1 varchar(250),--要比较的数据库名1
@dbname2 varchar(250)--要比较的数据库名2
as
set @dbname1='['+@dbname1+']'
set @dbname2='['+@dbname2+']'
 
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+'.dbo.sysobjects where xtype=''PK'' and parent_obj=a.id and name in (
SELECT name FROM '+@dbname1+'.dbo.sysindexes WHERE indid in(
SELECT indid FROM '+@dbname1+'.dbo.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+'.dbo.syscolumns a
left join '+@dbname1+'.dbo.systypes b on a.xtype=b.xusertype
inner join '+@dbname1+'.dbo.sysobjects d on a.id=d.id  and d.xtype=''U'' and  d.name<>''dtproperties''
left join '+@dbname1+'.dbo.syscomments e on a.cdefault=e.id
left join '+@dbname1+'.sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id  
order by a.id,a.colorder')
--select * from csdntest.sys.extended_properties
--得到数据库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+'.dbo.sysobjects where xtype=''PK'' and parent_obj=a.id and name in (
SELECT name FROM '+@dbname2+'.dbo.sysindexes WHERE indid in(
SELECT indid FROM '+@dbname2+'.dbo.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+'.dbo.syscolumns a
left join '+@dbname2+'.dbo.systypes b on a.xtype=b.xusertype
inner join '+@dbname2+'.dbo.sysobjects d on a.id=d.id  and d.xtype=''U'' and  d.name<>''dtproperties''
left join '+@dbname2+'.dbo.syscomments e on a.cdefault=e.id
left join '+@dbname2+'.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 @dbname1+'——缺少表:'+b.表名2
when b.表名2 is null and a.序号=1 then @dbname2+'——缺少表:'+a.表名1
when a.字段名 is null and exists(select 1 from #tb1 where 表名1=b.表名2) then 
'/*'+@dbname1+'——['+b.表名2+'] 缺少字段:'+b.字段名+'*/alter table '+b.表名2+' drop column '+b.字段名
when b.字段名 is null and exists(select 1 from #tb2 where 表名2=a.表名1) and a.类型='decimal' then 
'/*'+@dbname2+'——['+a.表名1+'] 缺少字段:'+a.字段名+'*/ alter table '+a.表名1+' add '+a.字段名+' '
+a.类型+'('+ltrim(str(a.长度))+','+ltrim(str(isnull(a.小数位数,'')))+')'+(case when a.允许空=0 then ' not null' else ' null' end)
when b.字段名 is null and exists(select 1 from #tb2 where 表名2=a.表名1) and a.类型='nvarchar' then 
'/*'+@dbname2+'——['+a.表名1+'] 缺少字段:'+a.字段名+'*/ alter table '+a.表名1+' add '+a.字段名+' '
+a.类型+'('+ltrim(str(a.长度))+')'+(case when a.允许空=0 then ' not null' else ' null' end)
when b.字段名 is null and exists(select 1 from #tb2 where 表名2=a.表名1) and a.类型 not in('nvarchar','decimal') then 
'/*'+@dbname2+'——['+a.表名1+'] 缺少字段:'+a.字段名+'*/ alter table '+a.表名1+' add '+a.字段名+' '
+a.类型+(case when a.允许空=0 then ' not null' else ' null' end)
when a.标识<>b.标识 then '--标识不同'
when a.主键<>b.主键 then '--主键设置不同'
when a.类型<>b.类型 and a.类型='decimal' then 
'/*字段类型不同*/ alter table '+a.表名1+' alter column '+a.字段名+' '+a.类型+'('+ltrim(str(a.长度))+','+ltrim(str(isnull(a.小数位数,'')))+')'+(case when a.允许空=0 then ' not null' else ' null' end)
when a.类型<>b.类型 and a.类型='nvarchar'  then 
'/*字段类型不同*/ alter table '+a.表名1+' alter column '+a.字段名+' '+a.类型+'('+ltrim(str(a.长度))+')'+(case when a.允许空=0 then ' not null' else ' null' end)
when a.类型<>b.类型 and a.类型 not in('nvarchar','decimal')  then 
'/*字段类型不同*/ alter table '+a.表名1+' alter column '+a.字段名+' '+a.类型+(case when a.允许空=0 then ' not null' else ' null' end)
when a.占用字节数<>b.占用字节数 then 
'/*占用字节数*/ alter table '+a.表名1+' alter column '+a.字段名+' '+a.类型+'('+ltrim(str(a.长度))+')'+(case when a.允许空=0 then ' not null' else ' null' end)
when a.长度<>b.长度 then 
'/*长度不同*/ alter table '+a.表名1+' alter column '+a.字段名+' '+a.类型+'('+ltrim(str(a.长度))+')'+(case when a.允许空=0 then ' not null' else ' null' end)
when a.小数位数<>b.小数位数 then 
'--小数位数不同*/ alter table '+a.表名1+' alter column '+a.字段名+' '+a.类型+'('+ltrim(str(a.长度))+','+ltrim(str(isnull(a.小数位数,'')))+')'+(case when a.允许空=0 then ' not null' else ' null' end)
when a.允许空<>b.允许空 then '--是否允许空不同'
when a.默认值<>b.默认值 then 
'/*默认值不同*/ alter table '+a.表名1+' add default(0) for '+a.字段名+' with values'
--when isnull(a.字段说明,'')<>'' and isnull(b.字段说明,'')='' then 
--'EXEC sys.sp_addextendedproperty @name=N'''+'MS_Description'+''', @value=N'''+a.字段说明--+''' , @level0type=N'''+'SCHEMA'+''',@level0name=N'''+'dbo'+''', @level1type=N'''+'TABLE'+''',@level1name=N'''+b.表名2+''', @level2type=N'''+'COLUMN'+''',@level2name=N'''+b.字段名+'''--字段说明不同'
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.字段名)






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值