日期:2014-05-17 浏览次数:20467 次
--A表的全部字段
select stuff((select ','+name from sys.columns where object_id=object_id('A') for xml path('')),1,1,'')
--AB两表相同的字段名
select a.name
(select name from sys.columns where object_id=object_id('A'))a
inner join
(select name from sys.columns where object_id=object_id('B'))b
on a.name=b.name
--AB两表不同的字段名,将AB俩表对换是另一种BA不同的字段
select name from sys.columns where object_id=object_id('A')
and name not in
(select name from sys.columns where object_id=object_id('B')
DECLARE @s NVARCHAR(4000)
SET @s = ''
SELECT @s = @s + ',' + QUOTENAME(name)
FROM syscolumns
WHERE id = OBJECT_ID('表名')
AND name NOT IN ( '不希望显式的列' )
SET @s = STUFF(@s, 1, 1, '')
EXEC('select '+@s+' from 表名 ')