存储过程,按照我的要求返回一个集合

ztingtingmck 2011-09-02 06:13:01
需求如下:
学生类型表:tb_studenttype
id name
001 类型1
002 类型2
003 类型3

学生表:tb_student

id name typeid
001 张三 001
002 李四 001
003 王五 002
004 李六 003


我要写一个存储过程,这个存储过程返回一个集合,格式如下:
-------------------------------
类型 学生编号 学生姓名
-------------------------------
类型1
-------------------------------
001 张三
002 李四
-------------------------------
类型2
-------------------------------
003 王五
-------------------------------
类型3
-------------------------------
004 李六
-------------------------------



...全文
61 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2011-09-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ztingtingmck 的回复:]
我想请教一下,楼上的order by 1,2或3,2都是什么意思啊
[/Quote]第1列,第2列,或第3,第2列.
ztingtingmck 2011-09-02
  • 打赏
  • 举报
回复
我想请教一下,楼上的order by 1,2或3,2都是什么意思啊
ztingtingmck 2011-09-02
  • 打赏
  • 举报
回复
楼上的解法类型好像只能有两个呀,超过两个数据好像就不对了喔
快溜 2011-09-02
  • 打赏
  • 举报
回复
declare @tb_studenttype table (id varchar(3),name varchar(5))
insert into @tb_studenttype
select '001','类型1' union all
select '002','类型2' union all
select '003','类型3'

declare @tb_student table (id varchar(3),name varchar(4),typeid varchar(3))
insert into @tb_student
select '001','张三','001' union all
select '002','李四','001' union all
select '003','王五','002' union all
select '004','李六','003'


select id,name as 类型,null as 学生编号,null as 学生姓名 from @tb_studenttype
union all
select a.id,null,b.id,b.name from @tb_studenttype a,@tb_student b where a.id=b.typeid
order by 1,2 desc

/*
id 类型 学生编号 学生姓名
---- ----- ---- ----
001 类型1 NULL NULL
001 NULL 001 张三
001 NULL 002 李四
002 类型2 NULL NULL
002 NULL 003 王五
003 类型3 NULL NULL
003 NULL 004 李六

(7 行受影响)
快溜 2011-09-02
  • 打赏
  • 举报
回复
select name as 类型,null as 学生编号,null as 学生姓名 from tb_studenttype
union all
select null,b.id,b.name from tb_studenttype a,tb_student b where a.id=b.typeid
order by 1
叶子 2011-09-02
  • 打赏
  • 举报
回复

declare @tb_studenttype table (id varchar(3),name varchar(5))
insert into @tb_studenttype
select '001','类型1' union all
select '002','类型2' union all
select '003','类型3'

declare @tb_student table (id varchar(3),name varchar(4),typeid varchar(3))
insert into @tb_student
select '001','张三','001' union all
select '002','李四','001' union all
select '003','王五','002' union all
select '004','李六','003'

select id=name,name=null,id from @tb_studenttype union all
select * from @tb_student
order by 3,2
/*
id name id
----- ---- ----
类型1 NULL 001
002 李四 001
001 张三 001
类型2 NULL 002
003 王五 002
类型3 NULL 003
004 李六 003
*/
-晴天 2011-09-02
  • 打赏
  • 举报
回复
报表的形式.

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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