go
调用语法:
exec 存储过程名称 实参,实参,实参 ...
--获取所有学员信息
if exists(select * from sysobjects where name='usp_getAllStuInfo')
drop proc usp_getAllStuInfo
go
create procedure usp_getAllStuInfo
as
select * from Student
go
--调用存储过程,获取的有学员信息
execute usp_getAllStuInfo
--exec sp_executesql 'select * from Student'
--查询指定性别的学员信息
go
if exists(select * from sysobjects where name='usp_getAllStuInfoBySex')
drop proc usp_getAllStuInfoBySex
go
create procedure usp_getAllStuInfoBySex
@sex nchar(1) --性别 参数不需要declare
as
select * from Student where Sex=@sex
go
--调用存储过程,获取指定性别的学员信息
Exec usp_getAllStuInfoBySex '女'
--创建存储过程获取指定班级和性别的学员信息
go
if exists(select * from sysobjects where name='usp_getAllStuInfoBySexandClassName')
drop proc usp_getAllStuInfoBySexandClassName
go
create procedure usp_getAllStuInfoBySexandClassName
@classname nvarchar(50), --班级名称
@sex nchar(1)='男'--性别 有默认的参数建议写在参数列表的最后
as
declare @classid int ---班级ID
set @classid=(select classid from grade where classname=@classname) --通过参数班级名称获取对应的班级ID
select * from Student where Sex=@sex and ClassId=@classid
go
--执行存储过程获取指定班级和性别的学员信息
--exec usp_getAllStuInfoBySexandClassName '八期班'
exec usp_getAllStuInfoBySexandClassName default, '八期班' --有默认值的参数可以传递default
exec usp_getAllStuInfoBySexandClassName @classname='八期班' --也可以通过参数=值的方式调用
exec usp_getAllStuInfoBySexandClassName @classname='八期班' ,@sex='女'
exec usp_getAllStuInfoBySexandClassName @classname='八期班',@sex='女'
--创建存储过程,获取指定性别的学员人数及总人数
go
if exists(select * from sysobjects where name='usp_getCountBySexandClassName')
drop proc usp_getCountBySexandClassName
go
create procedure usp_getCountBySexandClassName
@cnt int=100 output, --output标记说明它是一个输出参数。output意味着你向服务器请求这个参数的值,那么在执行的时候,服务器发现这个参数标记了output,就会将这个参数的值返回输出
@totalnum int =200output, --总人数
@className nvarchar(50), --输入参数没有默认值,在调用的时候必须传入值
@sex nchar(1)='男'--输入参数有默认值,用户可以选择是否传入值
as
declare @classid int ---班级ID
set @classid=(select classid from grade where classname=@classname) --通过参数班级名称获取对应的班级ID










