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

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

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

系統 2762 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元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产精品福利一区二区 | 亚洲线精品久久一区二区三区 | 黄片毛片一级片 | 久久香蕉国产线 | 热久久久久久 | 九九热国产视频 | 国产成人教育视频在线观看 | 精品国产一区二区三区成人 | 国产国语高清在线视频二区 | 国产精品一国产精品 | 四虎影视永久在线精品免费 | 91系列在线观看 | 久久国产成人亚洲精品影院老金 | 欧美亚洲国产色综合 | 国产成人免费在线视频 | 涩色网站| 爱性网| 狼狼色丁香久久女婷婷综合 | 在线观看片成人免费视频 | 在线观看国产精品入口 | 国产成年人在线观看 | 免费观看羞羞视频网站 | 国产一区二区精品久久小说 | 日韩亚洲欧美一区 | 日韩中文字幕在线亚洲一区 | 国产免费一级在线观看 | 亚洲欧美在线免费 | 综合久久精品 | www.色黄 | 97午夜视频 | 国产一级特黄高清在线大片 | 国产女人伦码一区二区三区不卡 | 性做久久久久久久免费看 | 欧美日韩激情在线 | aaaaaa国产毛片孕妇版 | 操操操干干 | 国产成人精品一区二三区在线观看 | 欧美黑人巨大肥婆性视频 | 国产精品久久久久乳精品爆 | 日韩福利影院 | 国产一区二区三区高清视频 |