如何比較兩個SQL數據庫的字段差別。


程序好幾個版本了,數據也弄出好好幾版本,這下好了,原程序要升級,當然數據庫也要升,可是里面已經有了大量的數據了,這時候怎么辦。寫了個存儲過程來解決,一目了然。

因為2005及以上的數據庫已經沒有表sysproperties了。所以。第一步,先創建一個。要比較的兩個表都需要:

執行以下代碼。

if exists (select 1 from sysobjects where name = 'sysproperties'and xtype = 'V')
begin
    DROP VIEW sysproperties
end
    GO
    CREATE VIEW sysproperties
AS
 SELECT class AS id,Minor_id AS sMallid,* from sys.extended_properties 

 

第二步,創建存儲過程。

GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER proc [dbo].[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(cast(g.[value] AS varchar(500)),'''')
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(cast(g.[value] AS varchar(500)),'''')
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 @dbname1+'缺少表:'+b.表名2
when b.表名2 is null and a.序號=1 then @dbname2+'缺少表:'+a.表名1
when a.字段名 is null and exists(select 1 from #tb1 where 表名1=b.表名2) then @dbname1+'.['+b.表名2+'] 缺少字段:'+b.字段名
when b.字段名 is null and exists(select 1 from #tb2 where 表名2=a.表名1) then @dbname2+' .['+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.字段名)

第三步,執行存儲過程。

exec p_comparestructure'hb','c'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM