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
在使用时,如果报错,报错提示如下:
SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ad Hoc Distributed Queries'。
加附一下语句运行一次:
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO