日期:2014-05-18  浏览次数:20501 次

我是新手 帮忙解一下啊
创建一个存储过程proc_GetStuInfo,输入参数为@Address,根据地址查询学生信息。将表头显示为中文,将性别“0”或“1”转换为“男”或“女”显示出来。如下图所示

  学号 姓名 年龄 性别 电话 住址
1 1 张三 20 男 13456780990 沙坪坝
2 2 美女 19 女 13256357854 沙坪坝

------解决方案--------------------
SQL code


--> 测试数据:[test]
if object_id('[test]') is not null 
drop table [test]
create table [test](
stuno int,
stuname varchar(4),
stuage int,
stusex tinyint,
stutel bigint,
stuaddress varchar(6)
)
insert [test]
select 1,'张三',20,1,13456780990,'沙坪坝' union all
select 2,'李四',20,1,13456780990,'沙坪坝' union all
select 3,'王五',20,1,13456780990,'开发区' union all
select 4,'美女',19,0,13256357854,'沙坪坝'

if OBJECT_ID('proc_GetStuInfo')is not null
drop proc proc_GetStuInfo
go
create proc proc_GetStuInfo @address nvarchar(20)
as
select 
     stuno as [学号],
     stuname as [姓名],
     stuage as [年龄],
     case when stusex=1 then '男' else '女' end as [性别],
     stutel as [电话],
     stuaddress as [地址]
from
     [test]
where 
     stuaddress=@address
go

--掉用存储过程:

exec proc_GetStuInfo '沙坪坝'

/*
学号    姓名    年龄    性别    电话    地址
---------------------------------------------------
1    张三    20    男    13456780990    沙坪坝
2    李四    20    男    13456780990    沙坪坝
4    美女    19    女    13256357854    沙坪坝
*/