输出列

dy91211310 2010-11-17 10:00:14
我有一条数据 里面200多个字段 有的有数据 有的没数据 现在我只想要有数据的展现出来 没数据的不看
求教!!
...全文
88 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
王向飞 2010-11-17
  • 打赏
  • 举报
回复
那还不如设成列形式的存储,列id 列属性 列值 [Quote=引用 4 楼 dy91211310 的回复:]
引用 2 楼 qianjin036a 的回复:
如果把没数据理解为某些列为空,可以这么写:
select * from 表 where 列名 is not null

这个适应于列明少的 自己能打 但我现在要求的是 200多列每个列都可能为空
然后输出不为空的列及对应的值 对应一条数据
[/Quote]
abuying 2010-11-17
  • 打赏
  • 举报
回复
/*运行环境 SQLSERVER2005*/
-- 构造数据

create table #T1
( a int null,
b int null,
c int null,
d int null,
e int null,
f int null,
g int null,
h int null,
i int null,
j int null
)

insert into #T1 select 1,null,null,null,null,null,null,null,null,null
insert into #T1 select null,2,null,null,null,null,null,null,null,null
insert into #T1 select null,null,3,null,null,null,null,null,null,null
insert into #T1 select null,null,null,4,null,null,null,null,null,null
insert into #T1 select null,null,null,null,5,null,null,null,null,null
insert into #T1 select null,null,null,null,null,6,null,null,null,null
insert into #T1 select null,null,null,null,null,null,7,null,null,null
insert into #T1 select null,null,null,null,null,null,null,8,null,null
insert into #T1 select null,null,null,null,null,null,null,null,9,null
insert into #T1 select null,null,null,null,null,null,null,null,null,10

declare @tbname varchar(100), -- 表名
@row int, -- 行数
@column int, -- 列数
@data varchar(100) -- 返回值

-- 手动为变量赋值
--**********--
select @tbname = '#T1'
select @row = 1
select @column = 1
--**********--

-- 判断行列值是否超出表范围
if ( @row > ( select count(1) from #T1 ) )
begin
print '输入行大于表最大行数'
end
select @tbname = 'tempdb..' + @tbname
if ( @column > ( select max(column_id)
from tempdb.sys.columns
where object_id = object_id(@tbname) ) )
begin
print '输入列大于表最大列数'
end

-- 定义动态SQL
declare @sql nvarchar(2000),
@parm nvarchar(500),
@col_name varchar(100)

while @column<=10
begin
select @col_name = name
from tempdb.sys.columns
where object_id = object_id(@tbname)
and column_id = @column

select @parm = '@val varchar(10) output'

select @sql = ' select @val = ' + @col_name +
' from ( select *,
row_number()over(order by getdate()) as ordr
from ' + @tbname +
' ) a
where ordr = ' + cast(@row as varchar)
exec sp_executesql @sql,
@parm,
@data output
--select @data
print @data
if @data is not null
begin
set @column=@column+1
end
else
break
end
dy91211310 2010-11-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 qianjin036a 的回复:]
如果把没数据理解为某些列为空,可以这么写:
select * from 表 where 列名 is not null
[/Quote]
这个适应于列明少的 自己能打 但我现在要求的是 200多列每个列都可能为空
然后输出不为空的列及对应的值 对应一条数据
dy91211310 2010-11-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ann725 的回复:]
什么数据,贴出来呗
[/Quote]
比如说一条数据
id fname lname age .......
1 fu null 23 ........
后面的省略号 代表 很多字段 和对应的数据 这是一个表中的,现在我要输出
的结果是
id fname age ...
1 fu 23 ...
数据中部位null的字段和对应的值
-晴天 2010-11-17
  • 打赏
  • 举报
回复
如果把没数据理解为某些列为空,可以这么写:
select * from 表 where 列名 is not null
Ann725 2010-11-17
  • 打赏
  • 举报
回复
什么数据,贴出来呗

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧