当前位置:Gxlcms > 数据库问题 > SQL Server比较2table字段的差异

SQL Server比较2table字段的差异

时间:2021-07-01 10:21:17 帮助过:16人阅读

  eg:select count(name)  from syscolumns where  id=object_id(‘t_dk‘)

2.查询数据库字段名 (表有哪些字段)

  select name  

  from 数据库名.dbo.syscolumns  

  where id=(

    select id from 数据库名.dbo.sysobjects  where name=‘表名‘

  )

  eg:

  select name 

  from Catsic_Compare0803DiLong_2017080311.dbo.syscolumns  

  where id=(

    select id from Catsic_Compare0803DiLong_2017080311.dbo.sysobjects  where name=‘t_cbjzc‘

  )

3.比较两个数据库相应表的差异(查询表对应的字段是否一致)

  本部分是基于2写的:

select * from (
  select name
  from 数据库A.dbo.syscolumns
  where id=(
    select id from 数据库A.dbo.sysobjects
    where name=‘表名A‘)
) T1 FULL OUTER JOIN(
  select name from 数据库B.dbo.syscolumns
  where id=(
    select id from 数据库B.dbo.sysobjects
    where name=‘表B‘
  )
) T2 on T1.name=T2.name

  eg:

select * from (
  select name 
  from Catsic_Compare0803DiLong_2017080311.dbo.syscolumns 
  where id=(
    select id from Catsic_Compare0803DiLong_2017080311.dbo.sysobjects 
    where name=‘t_cbjzc‘)
) T1 FULL OUTER JOIN(
  select name from Catsicgl_43_2016Eroad_2017111110.dbo.syscolumns 
  where id=(
    select id from Catsicgl_43_2016Eroad_2017111110.dbo.sysobjects 
    where name=‘t_cbjzc‘
  )
) T2 on T1.name=T2.name

只显示字段字段名有差异的字段,增加一个条件即可where T1.name is null or T2.name is null

即全部code:

select * from (
  select name 
  from Catsic_Compare0803DiLong_2017080311.dbo.syscolumns 
  where id=(
    select id from Catsic_Compare0803DiLong_2017080311.dbo.sysobjects 
    where name=‘t_cbjzc‘)
) T1 FULL OUTER JOIN(
  select name from Catsicgl_43_2016Eroad_2017111110.dbo.syscolumns 
  where id=(
    select id from Catsicgl_43_2016Eroad_2017111110.dbo.sysobjects 
    where name=‘t_cbjzc‘
  )
) T2 on T1.name=T2.name

where T1.name is null or T2.name is null

 

SQL Server初学者,鼓励转载,共同学习

 

SQL Server比较2table字段的差异

标签:column   显示   人工   objects   基于   serve   cat   com   统计   

人气教程排行