惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Webroot Blog
Webroot Blog
U
Unit 42
A
About on SuperTechFans
宝玉的分享
宝玉的分享
月光博客
月光博客
C
CERT Recently Published Vulnerability Notes
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
P
Privacy & Cybersecurity Law Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Securelist
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
B
Blog
I
Intezer
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
V
V2EX
L
LangChain Blog
AI
AI
G
GRAHAM CLULEY
T
Tor Project blog
人人都是产品经理
人人都是产品经理
D
Docker
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
I
InfoQ
Y
Y Combinator Blog
C
Comments on: Blog
GbyAI
GbyAI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
H
Help Net Security
Vercel News
Vercel News
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿

博客园 - 郄永军

用触发器实现表的同步操作 Quartz CronTrigger最完整配置说明 - 郄永军 - 博客园 如何优化JAVA程序开发,提高JAVA性能 使用Hibernate+Middlegen实现自动代码生成简介 如何用java调用c++编写成的DLL Web.xml加载顺序 java性能优化 Java NIO简介 选用ibatis和hibernate的区别 Fusioncharts 参数 dwr介绍 ASP.NET缓存:方法分析和实践示例 如何动态执行存储过程或函数 NTKO正文控件的使用技巧 - 郄永军 - 博客园 Dorado开发框架下保存附件存草稿功能 - 郄永军 - 博客园 JS中的关于类型转换的性能优化 - 郄永军 - 博客园 如何把ini文件转换为xml 生成随即的数值 获取文件大小的java程序
比较同一数据库不同版本间数据表之间差异
郄永军 · 2010-12-14 · via 博客园 - 郄永军

我们的项目在开发后期、或给用户升级系统时经常会碰到这样的问题:开发人员在修改数据库字段长度、更改数据库字段类型、添加主键等更改时 技术人员并没有把对数据库的操作及时、全面的记录下来。这样导致的后果是 实施人员在更新完系统后遗漏了对数据库某些更改,使系统报错。

以下脚本是自动比较该数据库两个版本之间存在的差异,并显示出来,该脚本适用与SQLSERVER 数据库。

例:比较当前库与服务器 192.168.1.1上库 aeronavy之间差异

if   exists(select * from master..sysservers where   srvname='192.168.1.1')

              begin

              EXEC sp_dropserver '192.168.1.1', 'droplogins'

              end

              --建立连接服务器

              EXEC sp_addlinkedserver

              --要创建的链接服务器名称

              --这里就用数据源作名称

              '192.168.1.1',

              'SQL Server'

              --创建链接服务器上远程登录之间的映射

              EXEC sp_addlinkedsrvlogin

              '192.168.1.1',

              'false',

              NULL,

              --远程服务器的登陆用户名

              'sa',

              --远程服务器的登陆密码

              'sa'

-------

--查询出俩库之间不同的表

select '新加表' as 操作, t.*

from

(select b.name as 表名,a.name as 列名,a.column_id,a.max_length,a.system_type_id,a.collation_name

from sys.all_columns as a,sys.all_objects as b

where a.object_id=b.object_id and b.type='U'

 )as t

where t.表名 not in (select q.name from [192.168.1.1].aeronavy.sys.all_objects as q where type='U')

order by t.表名,t.column_id asc

-----------------

--新库中删除的表

select '减少表' as 操作,q.name from [192.168.1.1].aeronavy.sys.all_objects as q

where q.name not in (select name from sys.all_objects where type='U')

and q.type='U'

-------------------------------------------------

--新库中删除的列

select '减少列' as 操作,q.*

from

(select b.name as 表名,a.name as 列名,a.column_id,a.max_length,a.system_type_id,a.collation_name

from [192.168.1.1].aeronavy.sys.all_columns as a,[192.168.1.1].aeronavy.sys.all_objects as b

where a.object_id=b.object_id and b.type='U'

 )as q

where q.表名 in (select t.表名 from (select b.name as 表名,a.name as 列名,a.column_id,a.max_length,a.system_type_id,a.collation_name

from sys.all_columns as a,sys.all_objects as b

where a.object_id=b.object_id and b.type='U') as t)

and q.列名 not in (

select t.列名 from (select b.name as 表名,a.name as 列名,a.column_id,a.max_length,a.system_type_id,a.collation_name

from sys.all_columns as a,sys.all_objects as b

where a.object_id=b.object_id and b.type='U') as t)

order by q.表名,q.column_id

---------------

-----查询出表名相同,列不同的字段

select '增加列' as 操作,t.*

from

(select b.name as 表名,a.name as 列名,a.column_id,a.max_length,a.system_type_id,a.collation_name

from sys.all_columns as a,sys.all_objects as b

where a.object_id=b.object_id and b.type='U'

 )as t

where t.表名 in (select q.name from [192.168.1.1].aeronavy.sys.all_objects as q)

and t.列名 not in (

select a.name as 列名

from [192.168.1.1].aeronavy.sys.all_columns as a,[192.168.1.1].aeronavy.sys.all_objects as b

where a.object_id=b.object_id and b.type='U'

and b.name=t.表名)

order by t.表名,t.column_id

--------------------------------------------------------------------

------列修改长度或类型

select '修改列' as 操作,t.*

from

(select b.name as 表名,a.name as 列名,a.column_id,a.system_type_id,a.max_length,a.precision,a.scale,a.collation_name

from sys.all_columns as a,sys.all_objects as b

where a.object_id=b.object_id and b.type='U'

 )as t,

(select b.name as 表名,a.name as 列名,a.column_id,a.system_type_id,a.max_length,a.precision,a.scale,a.collation_name

from [192.168.1.1].aeronavy.sys.all_columns as a,[192.168.1.1].aeronavy.sys.all_objects as b

where a.object_id=b.object_id and b.type='U'

 )as q

where q.表名=t.表名

and q.列名 = t.列名

and (q.system_type_id!=t.system_type_id or q.max_length!=t.max_length or q.precision!=t.precision or q.scale!=t.scale)

order by t.表名,t.column_id

--------------------------------------------------------------------------------------------

-----查询出 修改的主键

-------查看添加的主键.

select '增加主键' as 操作,t.* from

(select b.name as 表名,a.name as 列名,a.column_id,a.max_length,a.system_type_id,a.collation_name

from sys.all_columns as a,sys.all_objects as b,sys.index_columns as i

where a.object_id=b.object_id and b.type='U'

and i.object_id=a.object_id and i.column_id=a.column_id) as t

where t.表名 in(select q.表名 from (select b.name as 表名,a.name as 列名,a.column_id,a.max_length,a.system_type_id,a.collation_name

from [192.168.1.1].aeronavy.sys.all_columns as a,[192.168.1.1].aeronavy.sys.all_objects as b,[192.168.1.1].aeronavy.sys.index_columns as i

where a.object_id=b.object_id and b.type='U'

and i.object_id=a.object_id and i.column_id=a.column_id) as q)

and t.列名 not in (select q.列名 from (select b.name as 表名,a.name as 列名,a.column_id,a.max_length,a.system_type_id,a.collation_name

from [192.168.1.1].aeronavy.sys.all_columns as a,[192.168.1.1].aeronavy.sys.all_objects as b,[192.168.1.1].aeronavy.sys.index_columns as i

where a.object_id=b.object_id and b.type='U'

and i.object_id=a.object_id and i.column_id=a.column_id) as q)

------------------------------

--查看去掉的主键

select '删除主键' as 操作,q.* from

(select b.name as 表名,a.name as 列名,a.column_id,a.max_length,a.system_type_id,a.collation_name

from [192.168.1.1].aeronavy.sys.all_columns as a,[192.168.1.1].aeronavy.sys.all_objects as b,[192.168.1.1].aeronavy.sys.index_columns as i

where a.object_id=b.object_id and b.type='U'

and i.object_id=a.object_id and i.column_id=a.column_id) as q

where q.表名 in (select t.表名 from (select b.name as 表名,a.name as 列名,a.column_id,a.max_length,a.system_type_id,a.collation_name

from sys.all_columns as a,sys.all_objects as b,sys.index_columns as i

where a.object_id=b.object_id and b.type='U'

and i.object_id=a.object_id and i.column_id=a.column_id) as t)

and q.列名 not in

(select t.列名 from (select b.name as 表名,a.name as 列名,a.column_id,a.max_length,a.system_type_id,a.collation_name

from sys.all_columns as a,sys.all_objects as b,sys.index_columns as i

where a.object_id=b.object_id and b.type='U'

and i.object_id=a.object_id and i.column_id=a.column_id) as t)

备注:以上为完整脚本 可执行,上述脚本在编写时只考虑了功能的实现,并未考虑查询效率。