select 语句怎样剔除不想显示的列
请教各位,我有一个数据列较多的表,并且列是可以动态添加的。我想其中的三列不显示,因此用 select coulmn-name from table-name就没法实现
应该怎样写呢?
--test data prepare
create table test
(
ID int identity(1,1) primary key,
AA int,
BB int,
CC int,
)
insert into test
select 11,11,11 union all
select 22,22,22 union all
select 33,33,33
--query
declare @sql varchar(1000)
select @sql='select '
select @sql=@sql+name+',' from syscolumns where id=object_id(N'test') and name not in ('BB')
select @sql=left(@sql,len(@sql)-1)
select @sql=@sql+' from test'
exec (@sql)
/*
AA CC ID
----------- ----------- -----------
11 11 1
22 22 2
33 33 3
*/
--clear test data
drop table test