行列拆分转换

xiachangyu 2007-09-25 09:17:36
表test1
id tlgcl tlmoney scgcl scmoney
----------- ----------- ----------- ----------- -----------
1 20 200 30 300
2 40 400 50 500
要求写成函数; 当传入参数id=1时得到
name gcl money
---- ----------- -----------
sc 30 300
tl 20 200
id=2时得到:
name gcl money
---- ----------- -----------
sc 50 500
tl 40 400
...全文
244 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Limpire 2007-09-25
  • 打赏
  • 举报
回复
我没说只能写死
xiachangyu 2007-09-25
  • 打赏
  • 举报
回复
这样说只能写“死”了;谢谢了!!~~~
Limpire 2007-09-25
  • 打赏
  • 举报
回复
还是要用 UNION ALL 的,通过 syscolumns 表,根据字段名的规则,组装动态SQL。
xiachangyu 2007-09-25
  • 打赏
  • 举报
回复
to:Limpire(昨夜小楼)
是啊,字段动态!
Limpire 2007-09-25
  • 打赏
  • 举报
回复
明白了,字段动态是吧,不能用函数,只能用存储过程。
Limpire 2007-09-25
  • 打赏
  • 举报
回复
select * from fn_test1(1)
select * from fn_test1(2)

----------------

id 通过参数传进去,还不是动态吗

如果不是,想实现什么样的动态,告知。

另:函数里不支持 EXECUTE(字串)
xiachangyu 2007-09-25
  • 打赏
  • 举报
回复
当然上面的只是一个例子,如果我的表中字段有30个,一个一个的union all似乎有点不好,
因此想追求用最简的语句搞定
liehu1 2007-09-25
  • 打赏
  • 举报
回复
--创建测试表
create table a (id int,tlgcl int,tlmoney int,scgcl int,scmoney int)
insert into a select 1,20,200,30,300
union all select 2,40,400,50,500

--创建测试过程
create proc caifen
@id int
as
begin
select name='sc',gcl=scgcl,money=scmoney from a where id=@id
union all select name='tl', gcl=tlgcl,money=tlmoney from a where id=@id
end
xiachangyu 2007-09-25
  • 打赏
  • 举报
回复
to:marco08(天道酬勤)
能将下列语句
select id,gcl=tlgcl,[money]=tlmoney from test1
union all
select id,scgcl,scmoney from test1

写成动态吗?
xiachangyu 2007-09-25
  • 打赏
  • 举报
回复
to:Limpire(昨夜小楼)
可以将下列语句
select name='tl',gcl=tlgcl,money=tlmoney from test1 where id=@id
union all
select name='sc',gcl=scgcl,money=scmoney from test1 where id=@id
写成动态吗?
marco08 2007-09-25
  • 打赏
  • 举报
回复
--try

create table test1
(
id int,
tlgcl int,
tlmoney int,
scgcl int,
scmoney int
)

insert test1
select 1, 20, 200, 30, 300
union all
select 2, 40, 400, 50, 500

select * from
(
select id,gcl=tlgcl,[money]=tlmoney from test1
union all
select id,scgcl,scmoney from test1
)tmp
where id='1'
Limpire 2007-09-25
  • 打赏
  • 举报
回复
--创建测试
create table test1 (id int,tlgcl int,tlmoney int,scgcl int,scmoney int)
insert test1
select 1,20,200,30,300 union all
select 2,40,400,50,500
go

--创建内嵌表函数
create function fn_test1(@id int)
returns table
as
return
(
select name='tl',gcl=tlgcl,money=tlmoney from test1 where id=@id
union all
select name='sc',gcl=scgcl,money=scmoney from test1 where id=@id
)
go

--查询
select * from fn_test1(1) --笔误
select * from fn_test1(2)


--删除测试
/*
drop table test1
drop function fn_test1
*/
Limpire 2007-09-25
  • 打赏
  • 举报
回复
--创建测试
create table test1 (id int,tlgcl int,tlmoney int,scgcl int,scmoney int)
insert test1
select 1,20,200,30,300 union all
select 2,40,400,50,500
go

--创建内嵌表函数
create function fn_test1(@id int)
returns table
as
return
(
select name='tl',gcl=tlgcl,money=tlmoney from test1 where id=@id
union all
select name='sc',gcl=scgcl,money=scmoney from test1 where id=@id
)
go

--查询
select * from fn_test1(2)
select * from fn_test1(2)


--删除测试
/*
drop table test1
drop function fn_test1
*/

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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