如何根据记录条件返回字段名

jsxhxxp 2008-06-16 10:40:21
大家好:
有张表table1,结果如下
col0 col1 col2 col3
a null 1 null
b 1.25 null null
c null null 5

我想让查询结果插入到新表table2中
结果如下():
col0 colx
a col2
b col1
c col3
字段colx 中的记录col1,col2,col3即为table1中的字段名,根据table1中一条记录,如果这条记录中的字段col1,col2,col3中的值大于1,那么就显示其字段名在新表colx中
该如何实现上面的结果?
...全文
58 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2008-06-16
  • 打赏
  • 举报
回复
if not object_id('T') is null
drop table T
Go
Create table T([col0] nvarchar(1),[col1] decimal(18,2),[col2] int,[col3] int)
Insert T
select N'a',null,1,null union all
select N'b',1.25,null,null union all
select N'c',null,null,5

go
declare @s nvarchar(4000)
select
@s=isnull(@s,' case ')+' when '+quotename(Name)+' is not null then '+quotename(Name,'''')
from syscolumns
where ID=object_id('T') and Name not in('col0')

exec ('select [col0],COl='+@s+' end from T ')



(3 個資料列受到影響)
col0 COl
---- ----
a col2
b col1
c col3

(3 個資料列受到影響)
jsxhxxp 2008-06-16
  • 打赏
  • 举报
回复
wzy_love_sly方法可以,我去做做,其实问题并不是这么简单,这只是问题的一个重要部分,呵呵~有问题再问大家!
wzy_love_sly 2008-06-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jsxhxxp 的回复:]
我是要:
col0 colx
a col2
b col1
c col3
而不是:
col0 COl
---- ---------------------------------------
a 1.00
b 1.25
c 5.00
[/Quote]

3楼
中国风 2008-06-16
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20080612/22/c850499f-bce3-4877-82d5-af2357857872.html--列轉行
jsxhxxp 2008-06-16
  • 打赏
  • 举报
回复
我是要:
col0 colx
a col2
b col1
c col3
而不是:
col0 COl
---- ---------------------------------------
a 1.00
b 1.25
c 5.00

大侠们
中国风 2008-06-16
  • 打赏
  • 举报
回复

if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([col0] nvarchar(1),[col1] decimal(18,2),[col2] int,[col3] int)
Insert #T
select N'a',null,1,null union all
select N'b',1.25,null,null union all
select N'c',null,null,5
Go
Select col0,coalesce([col1],[col2],[col3]) as COl from #T


(3 個資料列受到影響)
col0 COl
---- ---------------------------------------
a 1.00
b 1.25
c 5.00

(3 個資料列受到影響)

wzy_love_sly 2008-06-16
  • 打赏
  • 举报
回复
create table tb(col0 varchar(50),col1 int,col2 int,col3 int)
insert into tb select 'a',null,1,null
insert into tb select 'b',1.25,null,null
insert into tb select 'c',null,null,5

select
col0,
colx=case when col1 is not null then 'col1' when col2 is not null then 'col2' when col3 is not null then 'col3' end
from tb


col0 colx
a col2
b col1
c col3
中国风 2008-06-16
  • 打赏
  • 举报
回复
把結果插入到table2
中国风 2008-06-16
  • 打赏
  • 举报
回复



if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([col0] nvarchar(1),[col1] decimal(18,2),[col2] int,[col3] int)
Insert #T
select N'a',null,1,null union all
select N'b',1.25,null,null union all
select N'c',null,null,5
Go
Select col0,[col1] from #T where COl1 is not null
union all
Select col0,[col2] from #T where COl2 is not null
union all
Select col0,[col3] from #T where COl3 is not null
order by col0



(3 個資料列受到影響)
col0 col1
---- ---------------------------------------
a 1.00
b 1.25
c 5.00

(3 個資料列受到影響)

22,207

社区成员

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

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