首页 新闻 会员 周边

求存储过程 返回临时表

0
悬赏园豆:10 [已解决问题] 解决于 2014-12-31 08:52

数据表结构如下:

1.我需要一个存储过程,取10条数据.返回的数据格式是临时表,字段有userID,userName,userTrueName,userBirthday共四个字段。
2.这10条数据需要满足下述条件:取姓为吴的取五条,如果不足五条的,有几条就取几条。即姓吴的取<=5条即可。
再用10减去上面取到的条数为b,再取出b条userBirthday>1990年的。
需存储过程取出数据表10条,满足条件1和2,求大神帮忙~

瑾的主页 | 菜鸟二级 | 园豆:332
提问于:2014-12-30 09:28
< >
分享
最佳答案
0

create proc p_SelectTenTable 

as

create table #temp(userID int,userName varchar(50),userTrueName varchar(50),userBirthday datetime)
declare @count int
 select count(*)=@count from table where stuName like 'wu%'
if(@count>5)
begin
set @count=5
insert into #temp select top 5 userID,userName,userTrueName,userBirthday  from table where stuName like 'wu%'
end
else
begin
insert into #temp select top 5 userID,userName,userTrueName,userBirthday  from table where stuName like 'wu%'
end
insert into #temp select top (10-@count) from table where year(userBirthday) > 1990
return #temp

收获园豆:10
隔壁老王来了 | 初学一级 |园豆:99 | 2014-12-30 10:00

存储过程啊,返回(方法里面用的)的了吗,直接后面 select * from #temp 

最后记得删除临时表,if ojbect_id('tempdb..#temp') is not null  drop table #temp

KingMi | 园豆:1344 (小虾三级) | 2014-12-30 10:45

@英雄莫问出处:可 他要求返回临时表啊,所以我就return了 有啥问题吗

隔壁老王来了 | 园豆:99 (初学一级) | 2014-12-30 10:59

@JaggerMan: 你执行下,看报错不是??

KingMi | 园豆:1344 (小虾三级) | 2014-12-30 11:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册