有什么简单好用的数据库结构对比工具?

asussony 2009-12-16 09:01:40
就是想比较本地和服务器上库结构不同,怕升级遗漏.

用过 StarInix Database Compare,但是有的时候,对比不出来,还有其他简单好用的么?
...全文
1029 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyzph 2009-12-17
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 7761098 的回复:]
引用 11 楼 drysea 的回复:
RED GATE 的SQL Compare,这一系列还有一个SQL DATA Cmopare,都挺好用的
最新版本支持sql2008
csdn有位GG破解地很好~
http://blog.csdn.net/Danny_Su/archive/2009/12/06/4953595.aspx


支持這個,我一直用
[/Quote]
这个不错。
忆轩辕 2009-12-16
  • 打赏
  • 举报
回复
我现在是导出建库脚本,然后文本比较=.=
happinessdepig 2009-12-16
  • 打赏
  • 举报
回复
学习...
drysea 2009-12-16
  • 打赏
  • 举报
回复
RED GATE 的SQL Compare,这一系列还有一个SQL DATA Cmopare,都挺好用的
最新版本支持sql2008
csdn有位GG破解地很好~
http://blog.csdn.net/Danny_Su/archive/2009/12/06/4953595.aspx
--小F-- 2009-12-16
  • 打赏
  • 举报
回复
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON
GO
create PROCEDURE r_pro_xx
@oldserver varchar(50),
@olddb varchar(50),
@olduser varchar(15),
@oldpw varchar(20),
@newserver varchar(50),
@newdb varchar(50),
@newuser varchar(15),
@newpw varchar(20),
@comtype char(2)
as
/*
比较两数据库的结构;
参数如下:
exec r_pro_xx
@oldserver='ma',--老的数据库的连接信息
@olddb ='olddb',
@olduser='sa',
@oldpw='a',
@newserver ='ma',--新的
@newdb='newdb',
@newuser='sa',
@newpw ='a',
@comtype ='p'--‘p’是比较的存储过程,‘u’是比较的表

注意:现在仅对表和存储过程进行比较,而且存储过程的比较还有缺陷,
缺陷如下:
如果两表中的同一存储过程,仅仅是空格或开始位置不一样,它也认为是不一样的..如何处理?
嘿嘿.
如果哪位有兴趣,帮改改哈.改完后联系我.
玉儿 QQ:49550145
*/
declare @sql varchar(8000)
declare @old varchar(8000),@new varchar(8000)
declare @i int, @j int ,@k int ,@leno int ,@lenn int
set @sql=''


create table #t_pro(name sysname null,bz varchar(50) null,old varchar(20),new varchar(20),lx varchar(10) null)
if @comtype ='p' or @comtype='tr' or @comtype='pk'
begin

create table #t_oldpro(name varchar(50) null,colid smallint not null ,text nvarchar(4000) null )
create table #t_newpro(name varchar(50) null,colid smallint not null ,text nvarchar(4000) null )
--存储过程
set @sql='insert into #t_oldpro
select * from OPENROWSET(''SQLOLEDB'','''+@oldserver+''';'''+@olduser+''';'''+@oldpw+''',
'' select name, b.colid,b.text from '+@olddb+'.dbo.sysobjects as a ,'+@olddb+'.dbo.syscomments as b
where A.category<>''''2'''' and a.xtype=''''p'''' and A.id=b.id '') '
exec(@sql)

set @sql='insert into #t_newpro
select * from OPENROWSET(''SQLOLEDB'','''+@newserver+''';'''+@newuser+''';'''+@newpw+''',
'' select name, b.colid,b.text from '+@newdb+'.dbo.sysobjects as a ,'+@newdb+'.dbo.syscomments as b
where A.category<>''''2'''' and a.xtype=''''p'''' and A.id=b.id '') '
exec(@sql)

set @sql='insert into #t_pro(name,bz,lx) select distinct name,''增加'',''存储过程'' from #t_newpro where name not in (select distinct name from #t_oldpro) '
exec(@sql)
delete from #t_newpro where name in ( select name from #t_pro where bz='增加')

set @sql='insert into #t_pro(name,bz,lx) select distinct name,''减少'',''存储过程'' from #t_oldpro where name not in (select distinct name from #t_newpro ) '
exec(@sql)
delete from #t_oldpro where name in ( select name from #t_pro where bz='减少')
--去掉增加减少的。只保留两个都有的。

-- for i=1 to
-- oobj.colid=nobj.colid and oobj.text<>nobj.text





set @sql='insert into #t_pro(name,bz,lx) select distinct nobj.name,''改动'',''存储过程'' from #t_oldpro oobj,#t_newpro nobj where oobj.name=nobj.name and oobj.colid=nobj.colid and oobj.text<>nobj.text'
exec (@sql)

--触发器 p存储过程,,tr触发器,pk主键约束,f,u表,d索引 v ,s 系统表,
drop table #t_oldpro
drop table #t_newpro
end

if @comtype ='u'
begin
create table #t_oldtab(tab sysname not null,name varchar(50) null,colid smallint not null ,domain int not null,type tinyint null,length smallint not null,
prec smallint null,scale int null,isnullable int null)
create table #t_newtab(tab sysname not null,name varchar(50) null,colid smallint not null ,domain int not null,type tinyint null,length smallint not null,
prec smallint null,scale int null,isnullable int null)
set @sql='insert into #t_oldtab
select * from OPENROWSET(''SQLOLEDB'','''+@oldserver+''';'''+@olduser+''';'''+@oldpw+''',
'' select a.name, b.name,b.colid,b.domain,b.type,b.length,b.prec,b.scale,b.isnullable from '+@olddb+'.dbo.sysobjects as a ,'+@olddb+'.dbo.syscolumns as b
where a.xtype=''''u'''' and A.id=b.id '') '
exec(@sql)


set @sql='insert into #t_newtab
select * from OPENROWSET(''SQLOLEDB'','''+@newserver+''';'''+@newuser+''';'''+@newpw+''',
'' select a.name ,b.name,b.colid,b.domain,b.type,b.length,b.prec,b.scale,b.isnullable from '+@newdb+'.dbo.sysobjects as a ,'+@newdb+'.dbo.syscolumns as b
where a.xtype=''''u'''' and A.id=b.id '') '
exec(@sql)

insert into #t_pro(name,bz,lx) select distinct tab,'减少表'+tab,'表' from #t_oldtab where tab not in (select distinct tab from #t_newtab)
insert into #t_pro(name,bz,lx) select distinct tab,'增加表'+tab,'表' from #t_newtab where tab not in (select distinct tab from #t_oldtab )
delete from #t_oldtab where tab in (select name from #t_pro where bz like '减少表%' )
delete from #t_newtab where tab in (select name from #t_pro where bz like '增加表%' )

insert into #t_pro(name,bz,lx) select new.name,new.tab+'增加列','表' from #t_newtab NEW where NEW.tab+NEW.name not in (select tab+name from #t_oldtab )
insert into #t_pro(name,bz,lx) select old.name,old.tab+'减少列','表' from #t_oldtab old where old.tab+old.name not in (select tab+name from #t_newtab )
insert into #t_pro(name,bz,old,new,lx)
select distinct nobj.name,'是否主键'+oobj.tab,cast(oobj.domain as char(10)),cast(nobj.domain as char(10)),'表' from #t_oldtab oobj,#t_newtab nobj where oobj.tab=nobj.tab and oobj.name=nobj.name and oobj.domain<>nobj.domain
union
select distinct nobj.name,'数据类型'+oobj.tab,cast(oobj.type as char(10)),cast(nobj.type as char(10)),'表' from #t_oldtab oobj,#t_newtab nobj where oobj.tab=nobj.tab and oobj.name=nobj.name and oobj.type<>nobj.type
union
select distinct nobj.name,'长度'+oobj.tab,cast(oobj.length as char(10)),cast(nobj.length as char(10)),'表' from #t_oldtab oobj,#t_newtab nobj where oobj.tab=nobj.tab and oobj.name=nobj.name and oobj.length<>nobj.length
union
select distinct nobj.name,'精度'+oobj.tab,cast(oobj.prec as char(10)),cast(nobj.prec as char(10)),'表' from #t_oldtab oobj,#t_newtab nobj where oobj.tab=nobj.tab and oobj.name=nobj.name and oobj.prec<>nobj.prec
union
select distinct nobj.name,'小数点位数'+oobj.tab,cast(oobj.scale as char(10)),cast(nobj.scale as char(10)),'表' from #t_oldtab oobj,#t_newtab nobj where oobj.tab=nobj.tab and oobj.name=nobj.name and oobj.scale<>nobj.scale
union
select distinct nobj.name,'是否为空'+oobj.tab,cast(oobj.isnullable as char(10)),cast(nobj.isnullable as char(10)),'表' from #t_oldtab oobj,#t_newtab nobj where oobj.tab=nobj.tab and oobj.name=nobj.name and oobj.isnullable<>nobj.isnullable

drop table #t_oldtab
drop table #t_newtab
end
select * from #t_pro
drop table #t_pro




GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

dawugui 2009-12-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 asussony 的回复:]
我的2个库在不同的机器上呢.
[/Quote]不同服务器数据库之间的数据操作

--创建链接服务器
exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 '
exec sp_addlinkedsrvlogin 'ITSV ', 'false ',null, '用户名 ', '密码 '

--查询示例
select * from ITSV.数据库名.dbo.表名

--导入示例
select * into 表 from ITSV.数据库名.dbo.表名

--以后不再使用时删除链接服务器
exec sp_dropserver 'ITSV ', 'droplogins '

--连接远程/局域网数据(openrowset/openquery/opendatasource)
--1、openrowset

--查询示例
select * from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)

--生成本地表
select * into 表 from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)

--把本地表导入远程表
insert openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)
select *from 本地表

--更新本地表
update b
set b.列A=a.列A
from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)as a inner join 本地表 b
on a.column1=b.column1

--openquery用法需要创建一个连接

--首先创建一个连接创建链接服务器
exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 '
--查询
select *
FROM openquery(ITSV, 'SELECT * FROM 数据库.dbo.表名 ')
--把本地表导入远程表
insert openquery(ITSV, 'SELECT * FROM 数据库.dbo.表名 ')
select * from 本地表
--更新本地表
update b
set b.列B=a.列B
FROM openquery(ITSV, 'SELECT * FROM 数据库.dbo.表名 ') as a
inner join 本地表 b on a.列A=b.列A

--3、opendatasource/openrowset
SELECT *
FROM opendatasource( 'SQLOLEDB ', 'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ' ).test.dbo.roy_ta
--把本地表导入远程表
insert opendatasource( 'SQLOLEDB ', 'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ').数据库.dbo.表名
select * from 本地表


wzy_love_sly 2009-12-16
  • 打赏
  • 举报
回复
tablediff实用工具
SQL Delta
guguda2008 2009-12-16
  • 打赏
  • 举报
回复
建立远程连接
asussony 2009-12-16
  • 打赏
  • 举报
回复
我的2个库在不同的机器上呢.
薪水 2009-12-16
  • 打赏
  • 举报
回复
学习
dawugui 2009-12-16
  • 打赏
  • 举报
回复
/*--数据结构比较

比较两个数据库中的视图/存储过程的结构(结构比较,不是功能比较)

--邹建 2004.07(引用请保留此信息)--*/

/*--调用示例

--调用
exec p_compdb 'pubs','northwind'
--*/
create proc p_compdb
@db1 sysname, --第一个库
@db2 sysname --第二个库
as
exec('
select 类型=case isnull(a.xtype,b.xtype) when ''V'' 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)
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

guguda2008 2009-12-16
  • 打赏
  • 举报
回复
不懂帮顶学习蹭分
dawugui 2009-12-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 asussony 的回复:]
就是想比较本地和服务器上库结构不同,怕升级遗漏.

用过 StarInix Database Compare,但是有的时候,对比不出来,还有其他简单好用的么?
[/Quote]

表结构比较:
-------------------------------------------------------------------------------------------
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_comparestructure]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_comparestructure]
GO

/*--比较两个数据库的表结构差异

可以比较两个数据库的结构差异

--邹建 2003.9(引用请保留此信息)--*/

/*--调用示例

exec p_comparestructure '库1','库2'
--*/
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 '+@dbname1+'..sysproperties g on a.id=g.id and a.colid=g.smallid
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 '+@dbname2+'..sysproperties g on a.id=g.id and a.colid=g.smallid
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缺少表:'+b.表名2
when b.表名2 is null and a.序号=1 then '库2缺少表:'+a.表名1
when a.字段名 is null and exists(select 1 from #tb1 where 表名1=b.表名2) then '库1 ['+b.表名2+'] 缺少字段:'+b.字段名
when b.字段名 is null and exists(select 1 from #tb2 where 表名2=a.表名1) then '库2 ['+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

Dream_1986 2009-12-16
  • 打赏
  • 举报
回复
学习
7761098 2009-12-16
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 drysea 的回复:]
RED GATE 的SQL Compare,这一系列还有一个SQL DATA Cmopare,都挺好用的
最新版本支持sql2008
csdn有位GG破解地很好~
http://blog.csdn.net/Danny_Su/archive/2009/12/06/4953595.aspx
[/Quote]

支持這個,我一直用
szpilmany 2009-12-16
  • 打赏
  • 举报
回复
VS2008,支持数据库结构,数据的对比

34,623

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧