首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求对比两个表中,字段名不一样的SQL,该怎么处理

2011-12-30 
求对比两个表中,字段名不一样的SQL需要包括有几种情况一、A表中有的字段B表无二、B表有的A表无三、两个表字段

求对比两个表中,字段名不一样的SQL
需要包括有几种情况
一、A表中有的字段B表无
二、B表有的A表无
三、两个表字段名不一致的


[解决办法]
如果只对比字段名,可以这样

一、A表中有的字段B表无
select name from syscolumns where id=object_id( 'A ') and name not in(select name from syscolumns where id=object_id( 'B '))
二、B表有的A表无
select name from syscolumns where id=object_id( 'B ') and name not in(select name from syscolumns where id=object_id( 'A '))
三、两个表字段名不一致的
select name from syscolumns where id=object_id( 'A ') and name not in(select name from syscolumns where id=object_id( 'B '))
union
select name from syscolumns where id=object_id( 'B ') and name not in(select name from syscolumns where id=object_id( 'A '))
[解决办法]
如楼上所说,在系统表syscolumns 中找出表A的字段记录(方法为SELECT * FROM syscolumns WHERE id = object_id(N 'TableA ') )
用同样的方法在进行sql处理就可以了

热点排行