Sql Server 数据库表结构,存储过程,视图比较脚本
顶级干货
用来比较两个数据库之间 表结构,存储过程及视图差异的存储过程,直接复制对应的存储过程,无需改动,直接在数据库中执行(传递要比较的数据库参数)即可
1.两个数据库之间存储过程及视图差异比较的存储过程
--测试脚本
--exec [p_compSPAndView] 'FAMS_PrePROD','FAMS_SIT' SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE?proc
[dbo].[p_compSPAndView] @db1
sysname, --第一个库 @db2
sysname --第二个库 as exec(' select
类型=case isnull(a.xtype,b.xtype) when ''V''
then N''视图''
else N''存储过程''
end ,匹配情况=case when
a.name is null then N''库
['+@db1+']
中无'' when
b.name is null then N''库
['+@db2+']
中无'' else
N''结构不同''
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'')
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'')
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 类型,匹配情况,对象名称
GO
效果如下:
2.两个数据库之间表结构差异比较的存储过程
--测试脚本
--exec p_comparestructure 'Inventory_SR08','Inventory_SR08_bk' -- =============================================
-- Author:
-- Description: <比较两个数据库的表结构差异并自动生成修改和插入表字段脚本>
--exec p_comparestructure 'Inventory_SR08','Inventory_SR08_bk'
-- =============================================
Create 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.字段名)
效果如下:取第一个为例,表名1(FAMS_PRD_TRUNNK数据库下) 的 AMT_AssetRetirement 表有 HandleOption 字段,而FAMS数据库下的 AMT_AssetRetirement 表不存在 HandleOption 字段
Sql Server 数据库表结构,存储过程,视图比较脚本的更多相关文章
- 获取sql server数据库表结构
if exists (select 1 from sysobjects where name = 'sysproperties'and xtype = 'V')begin DROP VIEW s ...
- SQL Server查看表结构及视图,适合开发者使用,简单易用
SELECT * FROM INFORMATION_SCHEMA.TABLES SELECT * FROM INFORMATION_SCHEMA.COLUMNS 查看执行结果
- 查询Sql Server数据库对象结构
查询Sql Server数据库对象结构 查询数据库 查询架构 查询表 查询列 查询存储过程 查询视图 1.查询某一服务器下所有数据库 select t.[name] as 数据库 from sys.d ...
- 千万级SQL Server数据库表分区的实现
千万级SQL Server数据库表分区的实现 2010-09-10 13:37 佚名 数据库 字号:T | T 一般在千万级的数据压力下,分区是一种比较好的提升性能方法.本文将介绍SQL Server ...
- SQL SERVER 数据库表同步复制 笔记
SQL SERVER 数据库表同步复制 笔记 同步复制可运行在不同版本的SQL Server服务之间 环境模拟需要两台数据库192.168.1.1(发布),192.168.1.10(订阅) 1.在发布 ...
- SQL Server 修改表结构(转载)
SQL Server 修改表结构 本文链接:https://blog.csdn.net/petezh/article/details/81744374 查看指定表结构 exec sp_help Rep ...
- EF Core中,通过实体类向SQL Server数据库表中插入数据后,实体对象是如何得到数据库表中的默认值的
我们使用EF Core的实体类向SQL Server数据库表中插入数据后,如果数据库表中有自增列或默认值列,那么EF Core的实体对象也会返回插入到数据库表中的默认值. 下面我们通过例子来展示,EF ...
- 修改SQL Server数据库表的创建时间最简单最直接有效的方法
说明:这篇文章是几年前我发布在网易博客当中的原创文章,但由于网易博客现在要停止运营了,所以我就把这篇文章搬了过来,因为这种操作方式是通用的,即使是对现在最新的SQL Server数据库里面的操作也是一 ...
- SQL Server修改表结构后批量更新所有视图
最近修改了数据库表结构,数据同步的时候出了问题,发现很多数据明明已经修改,但是通过视图筛选出来的还是原来的数据,所以怀疑应该是视图缓存了数据,在园子里找到下面的博文,在这里做个记录备忘. 原文链接:h ...
随机推荐
- [转] Vmware vs Virtualbox vs KVM vs XEN: virtual machines performance comparison
http://www.ilsistemista.net/index.php/virtualization/1-virtual-machines-performance-comparison.html? ...
- 背水一战 Windows 10 (119) - 后台任务: 后台下载任务(任务分组,组完成后触发后台任务)
[源码下载] 背水一战 Windows 10 (119) - 后台任务: 后台下载任务(任务分组,组完成后触发后台任务) 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 后台下 ...
- 每天学点SpringCloud(八):使用Apollo做配置中心
由于Apollo支持的图形化界面相对于我们更加的友好,所以此次我们使用Apollo来做配置中心 本篇文章实现了使用Apollo配置了dev和fat两个环境下的属性配置.Apollo官方文档https: ...
- Java 虚拟机的垃圾回收
背景 垃圾收集(Garbage Collection,GC),GC的历史比Java久远,1960年诞生于MIT的Lisp是第一门真正使用内存动态分配和垃圾收集技术的语言. 对于Java来说,运行时区域 ...
- 使用本地缓存快还是使用redis缓存好?
使用本地缓存快还是使用redis缓存好? Redis早已家喻户晓,其性能自不必多说. 但是总有些时候,我们想把性能再提升一点,想着redis是个远程服务,性能也许不够,于是想用本地缓存试试!想法是不错 ...
- shell 中的单行注释和多行注释
1. 单行注释 众所周知,# 比如想要注释:echo “ni” # echo "ni" 2. 多行注释: 法一: : << ! 语句1 语句2 语句3 语句4 ! 例 ...
- 什么 是JavaScript中的字符串类型之间的转换问题详解? 部分4
字符串类型 单双引号都可以!建议使用单引号!(本人建议:个人觉得单个字符串更利于网页优化@特别地方特别处理!); 判断字符串的长度获取方式:变量名.length html中转义符: < < ...
- axios的秘密
vue自2.0开始,vue-resource不再作为官方推荐的ajax方案,转而推荐使用axios. 按照作者的原话来说: “Ajax 本身跟 Vue 并没有什么需要特别整合的地方,使用 fetch ...
- 安装MySQL时候最后一步报无法定位程序输入点fesetround于动态链接库MSVCR120.dll
今天在装MySQL时到最后一步出现了一个问题[报无法定位程序输入点fesetround于动态链接库MSVCR120.dll]这是由什么原因引起的呢,其实是缺少一个vcredist_x64.exe插件 ...
- 【emotion】目标初定
现在的我漂浮不定,我的心是凌乱的,虽然在我面前的路有无数条,但是我却不知道哪一条路是属于我的.对于java,我掌握的东西可能并不是系统的,想想也知道,自学一年,能形成什么样子的体系呢?然而在日常的工作 ...