注册 登录
编程论坛 SQL Server论坛

select 语句怎样剔除不想显示的列

littleskirt 发布于 2007-01-07 19:39, 2166 次点击
请教各位,我有一个数据列较多的表,并且列是可以动态添加的。我想其中的三列不显示,
因此用 select coulmn-name from table-name就没法实现
应该怎样写呢?
7 回复
#2
棉花糖ONE2007-01-07 19:43

你不选那三列不行么

#3
littleskirt2007-01-07 19:49
不行啊,我的列是可以动态添加的,不知道用户会添加什么样的列阿。
#4
xu20002007-01-07 20:30

不明白,你的列是怎么个动态添加的?

#5
angeloc2007-01-07 21:55
能动态添加,当然也可以动态改变显示列了。这个没什么困难吧?
#6
LouisXIV2007-01-08 10:08

--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

#7
Kendy1234562007-01-08 16:27
同意楼上的方案
简单的说 就是在sql下也可以用字符串方式 拼接出一条你需要的sql 语句 然后用exec (@sqlstring)的方式执行

更简单的方式
是在vb代码中直接动态生成sql语句 然后调用fill dataset返回结果集 每次你不想显示哪几列 生成的sql语句中就不要包括那几列
#8
yygfml2007-01-22 21:38
还可以用创建view视图来做一个虚拟的表来显示你需要的列
1