首页 > 开发 > 综合 > 正文

MS SQL新旧库数据字典比较脚本

2024-07-21 02:11:56
字体:
来源:转载
供稿:网友
     /*ms sql新旧库数据字典比较脚本*/

--注明1:新旧库必须在同一数据库服务器同一实例中,最好以sa身份登入。
--注明2:本脚本可作为系统升级改造,得到相关信息后作数据迁移之用。
declare @i int

set @i=4  /*注明3:1为要得到新库增加的数据字典信息;
                 2为要得到旧库多出的数据字典信息;
                 3为要得到新库增加的表的数据字典信息;
                 4为要得到旧库多出的表的数据字典信息 */
               

use temp  --打开旧库
select sysobjects.name as [table], case when cast(sysproperties.[value] as varchar)
      is null then '' else cast(sysproperties.[value] as varchar) end as 表说明,
      syscolumns.name as field, case when cast(properties.[value] as varchar) is null
      then '' else cast(properties.[value] as varchar) end as 字段说明,
      systypes.name as type, syscolumns.length,
      isnull(columnproperty(syscolumns.id, syscolumns.name, 'scale'), 0)
      as 小数位数, syscolumns.isnullable as isnull,
      case when syscomments.text is null
      then '' else syscomments.text end as [default],
      case when columnproperty(syscolumns.id, syscolumns.name, 'isidentity')
      = 1 then '√' else '' end as 标识, case when exists
          (select 1
         from sysobjects
         where xtype = 'pk' and name in
                   (select name
                  from sysindexes
                  where indid in
                            (select indid
                           from sysindexkeys
                           where id = syscolumns.id and colid = syscolumns.colid)))
      then '√' else '' end as 主键 into #old
from syscolumns inner join
      sysobjects on sysobjects.id = syscolumns.id inner join
      systypes on syscolumns.xtype = systypes.xtype left outer join
      sysproperties properties on syscolumns.id = properties.id and
      syscolumns.colid = properties.smallid left outer join
      sysproperties on sysobjects.id = sysproperties.id and
      sysproperties.smallid = 0 left outer join
      syscomments on syscolumns.cdefault = syscomments.id
where (sysobjects.xtype = 'u')


use accdb --打开新库
select sysobjects.name as [table], case when cast(sysproperties.[value] as varchar)
      is null then '' else cast(sysproperties.[value] as varchar) end as 表说明,
      syscolumns.name as field, case when cast(properties.[value] as varchar) is null
      then '' else cast(properties.[value] as varchar) end as 字段说明,
      systypes.name as type, syscolumns.length,
      isnull(columnproperty(syscolumns.id, syscolumns.name, 'scale'), 0)
      as 小数位数, syscolumns.isnullable as isnull,
      case when syscomments.text is null
      then '' else syscomments.text end as [default],
      case when columnproperty(syscolumns.id, syscolumns.name, 'isidentity')
      = 1 then '√' else '' end as 标识, case when exists
          (select 1
         from sysobjects
         where xtype = 'pk' and name in
                   (select name
                  from sysindexes
                  where indid in
                            (select indid
                           from sysindexkeys
                           where id = syscolumns.id and colid = syscolumns.colid)))
      then '√' else '' end as 主键 into #new
from syscolumns inner join
      sysobjects on sysobjects.id = syscolumns.id inner join
      systypes on syscolumns.xtype = systypes.xtype left outer join
      sysproperties properties on syscolumns.id = properties.id and
      syscolumns.colid = properties.smallid left outer join
      sysproperties on sysobjects.id = sysproperties.id and
      sysproperties.smallid = 0 left outer join
      syscomments on syscolumns.cdefault = syscomments.id
where (sysobjects.xtype = 'u') 


if @i=1
  begin
    select n.* --新库与旧库相比较后新库增加的数据字典信息
     from #new n left join #old o on n.[table]=o.[table] and n.field=o.field where o.[table] is null
    or o.field is null order by n.[table],n.field
  end
  else
   begin
     if @i=2
       begin
         select o.* --新库与旧库相比较后旧库多出的数据字典信息
          from #new n right join #old o on n.[table]=o.[table] and n.field=o.field where n.[table] is null
           or n.field is null order by o.[table],o.field
       end
       else
         begin
         if @i=3
           begin
             select * --新库与旧库相比较后新库增加的表的数据字典信息
             from #new where [table] <> all(select [table] from #old ) order by [table],field
           end
           else
             begin
              if @i=4
                begin
                  select * --新库与旧库相比较后旧库多出的表的数据字典信息
                  from #old where [table] <> all(select [table] from #new ) order by [table],field
                end
                else
                  begin
                    select '出错啦'
                  end    
             end    
         end   
   end

drop table #old
drop table #new


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表