/*--比較兩個數據庫的表結構差異--鄒建2003.9(引用請保留此信息)--*//*--調用示例execp_comparestruc" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

比較兩個數據庫的表結構差異

系統 2694 0
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>

/*--比較兩個數據庫的表結構差異

--鄒建 2003.9(引用請保留此信息)--*/
/*--調用示例

exec p_comparestructure 'xzkh_model','xzkh_new'
--*/

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

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,默認值 varchar(500),字段說明 varchar(500))

create table #tb2(表名2 varchar(250),字段名 varchar(250),序號 int,標識 bit,主鍵 bit,類型 varchar(250),
占用字節數 int,長度 int,小數位數 int,允許空 bit,默認值 varchar(500),字段說明 varchar(500))

--得到數據庫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 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 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




/*--比較兩個數據庫的表結構差異

--鄒建 2003.9(引用請保留此信息)--*/
/*--調用示例

exec p_comparestructure 'xzkh_model','xzkh_new'
--*/

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

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,默認值 varchar(500),字段說明 varchar(500))

create table #tb2(表名2 varchar(250),字段名 varchar(250),序號 int,標識 bit,主鍵 bit,類型 varchar(250),
占用字節數 int,長度 int,小數位數 int,允許空 bit,默認值 varchar(500),字段說明 varchar(500))

--得到數據庫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 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 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




/*--比較兩個數據庫的表結構差異

--鄒建 2003.9(引用請保留此信息)--*/
/*--調用示例

exec p_comparestructure 'xzkh_model','xzkh_new'
--*/

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

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,默認值 varchar(500),字段說明 varchar(500))

create table #tb2(表名2 varchar(250),字段名 varchar(250),序號 int,標識 bit,主鍵 bit,類型 varchar(250),
占用字節數 int,長度 int,小數位數 int,允許空 bit,默認值 varchar(500),字段說明 varchar(500))

--得到數據庫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 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 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




/*--比較兩個數據庫的表結構差異

--鄒建 2003.9(引用請保留此信息)--*/
/*--調用示例

exec p_comparestructure 'xzkh_model','xzkh_new'
--*/

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

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,默認值 varchar(500),字段說明 varchar(500))

create table #tb2(表名2 varchar(250),字段名 varchar(250),序號 int,標識 bit,主鍵 bit,類型 varchar(250),
占用字節數 int,長度 int,小數位數 int,允許空 bit,默認值 varchar(500),字段說明 varchar(500))

--得到數據庫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 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 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




/*--比較兩個數據庫的表結構差異

--鄒建 2003.9(引用請保留此信息)--*/
/*--調用示例

exec p_comparestructure 'xzkh_model','xzkh_new'
--*/

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

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,默認值 varchar(500),字段說明 varchar(500))

create table #tb2(表名2 varchar(250),字段名 varchar(250),序號 int,標識 bit,主鍵 bit,類型 varchar(250),
占用字節數 int,長度 int,小數位數 int,允許空 bit,默認值 varchar(500),字段說明 varchar(500))

--得到數據庫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 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 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




/*--比較兩個數據庫的表結構差異

--鄒建 2003.9(引用請保留此信息)--*/
/*--調用示例

exec p_comparestructure 'xzkh_model','xzkh_new'
--*/

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

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,默認值 varchar(500),字段說明 varchar(500))

create table #tb2(表名2 varchar(250),字段名 varchar(250),序號 int,標識 bit,主鍵 bit,類型 varchar(250),
占用字節數 int,長度 int,小數位數 int,允許空 bit,默認值 varchar(500),字段說明 varchar(500))

--得到數據庫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 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 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




/*--比較兩個數據庫的表結構差異

--鄒建 2003.9(引用請保留此信息)--*/
/*--調用示例

exec p_comparestructure 'xzkh_model','xzkh_new'
--*/

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

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,默認值 varchar(500),字段說明 varchar(500))

create table #tb2(表名2 varchar(250),字段名 varchar(250),序號 int,標識 bit,主鍵 bit,類型 varchar(250),
占用字節數 int,長度 int,小數位數 int,允許空 bit,默認值 varchar(500),字段說明 varchar(500))

--得到數據庫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 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 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




/*--比較兩個數據庫的表結構差異

--鄒建 2003.9(引用請保留此信息)--*/
/*--調用示例

exec p_comparestructure 'xzkh_model','xzkh_new'
--*/

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

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,默認值 varchar(500),字段說明 varchar(500))

create table #tb2(表名2 varchar(250),字段名 varchar(250),序號 int,標識 bit,主鍵 bit,類型 varchar(250),
占用字節數 int,長度 int,小數位數 int,允許空 bit,默認值 varchar(500),字段說明 varchar(500))

--得到數據庫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 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 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




/*--比較兩個數據庫的表結構差異

--鄒建 2003.9(引用請保留此信息)--*/
/*--調用示例

exec p_comparestructure 'xzkh_model','xzkh_new'
--*/

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

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,默認值 varchar(500),字段說明 varchar(500))

create table #tb2(表名2 varchar(250),字段名 varchar(250),序號 int,標識 bit,主鍵 bit,類型 varchar(250),
占用字節數 int,長度 int,小數位數 int,允許空 bit,默認值 varchar(500),字段說明 varchar(500))

--得到數據庫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 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 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




/*--比較兩個數據庫的表結構差異

--鄒建 2003.9(引用請保留此信息)--*/
/*--調用示例

exec p_comparestructure 'xzkh_model','xzkh_new'
--*/

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

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,默認值 varchar(500),字段說明 varchar(500))

create table #tb2(表名2 varchar(250),字段名 varchar(250),序號 int,標識 bit,主鍵 bit,類型 varchar(250),
占用字節數 int,長度 int,小數位數 int,允許空 bit,默認值 varchar(500),字段說明 varchar(500))

--得到數據庫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 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 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




/*--比較兩個數據庫的表結構差異

--鄒建 2003.9(引用請保留此信息)--*/
/*--調用示例

exec p_comparestructure 'xzkh_model','xzkh_new'
--*/

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

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,默認值 varchar(500),字段說明 varchar(500))

create table #tb2(表名2 varchar(250),字段名 varchar(250),序號 int,標識 bit,主鍵 bit,類型 varchar(250),
占用字節數 int,長度 int,小數位數 int,允許空 bit,默認值 varchar(500),字段說明 varchar(500))

--得到數據庫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 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 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




/*--比較兩個數據庫的表結構差異

--鄒建 2003.9(引用請保留此信息)--*/
/*--調用示例

exec p_comparestructure 'xzkh_model','xzkh_new'
--*/

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

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,默認值 varchar(500),字段說明 varchar(500))

create table #tb2(表名2 varchar(250),字段名 varchar(250),序號 int,標識 bit,主鍵 bit,類型 varchar(250),
占用字節數 int,長度 int,小數位數 int,允許空 bit,默認值 varchar(500),字段說明 varchar(500))

--得到數據庫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 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 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




/*--比較兩個數據庫的表結構差異

--鄒建 2003.9(引用請保留此信息)--*/
/*--調用示例

exec p_comparestructure 'xzkh_model','xzkh_new'
--*/

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

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,默認值 varchar(500),字段說明 varchar(500))

create table #tb2(表名2 varchar(250),字段名 varchar(250),序號 int,標識 bit,主鍵 bit,類型 varchar(250),
占用字節數 int,長度 int,小數位數 int,允許空 bit,默認值 varchar(500),字段說明 varchar(500))

--得到數據庫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 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

分享到:
評論

比較兩個數據庫的表結構差異


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 欧美成一级 | 免费看国产一级特黄aa大片 | 日韩一区二区三区视频在线观看 | 在线免费观看中文字幕 | 亚洲精品二三区伊人久久 | 国产精品400部自产在线观看 | 91久草视频| 免费网站啪啪大全 | 国产精品每日更新在线观看 | 狠狠色噜噜狠狠色综合久 | 久久99国产精一区二区三区 | 国产中文字幕视频 | 国产精品欧美亚洲 | 国模和精品嫩模私拍视频 | 日本黄色录像 | 国产激情久久久久久影院 | 2021最新国产成人精品免费 | 天天插天天射天天干 | 久久成人免费观看全部免费 | 免费视频一区二区性色 | 日日摸夜夜爽 | 亚洲一区二区天海翼 | 香蕉视频网站免费观视频 | 成年ssswww中国女人 | 女人a级毛片| 在线看片不卡 | 欧美综合图片 | 素人巨乳被调教 | 久操综合在线 | 欧美在线视频在线观看 | xxx国产老太婆视频 xxx毛片 | 久久99精品国产 | 久热操| 亚洲人成网站在线观看青青 | 日韩中文字幕高清在线专区 | 国产成人亚洲精品老王 | 日韩黄色精品 | 国产99欧美精品久久精品久久 | 91国视频 | 欧洲成人爽视频在线观看 | 国产福利一区二区在线精品 |