sqlserver 比较两个表的列
一、问题
给了两个各有四五十个列的表,找出他们相同的列和不同的列
二、查询两个表的列,存在临时表
--#a ,#b都是临时表,当前连接断开后自动删除
--RANK() OVER (ORDER BY syscolumns.name DESC) AS 是SQL2005支持的,在每行记录前加上自增序号
--IDENTITY(INT,1,1) 函数必须要和into联合使用
1、将表的列存入#a--'destTbl'比较的表名
select * into #a from (select RANK() OVER (ORDER BY syscolumns.name DESC) AS 序号,syscolumns.name
from syscolumns,sysobjects
where syscolumns.[id]=sysobjects.[id]
and sysobjects.[name]='destTbl') as t
select * from #a
1 姓名
2 课程
3 id
4 cno
2、将表的列存入#b--'student'比较的表名
select 序号= IDENTITY(INT,1,1),syscolumns.name
into #b from syscolumns,sysobjects
where syscolumns.[id]=sysobjects.[id]
and sysobjects.[name]='student'
select * from #b
1 id
2 name
3 cno
三、分析比较各个表列的异同
用下列语句,或者稍作改动比较
select * from #b where name in (select name from #a)
select * from #a where name not in (select name from #b)
select * from #a a, #b b where a.name=b.name
select * from #a a left join #b b on a.name=b.name