- 创建存储过程
语句:
create procedure 存储过程名
[{@参数名 参数类型} [=默认值] [output]] [···n]
[with {recomple|encription|recom[ile,encription}]
as sql语句[···n]
其中:
output:该参数在存储过程退出后,其值将返回至调用程序,以便在调用该存储过程的程序中获得并使用该参数值
recomple:创建的存储过程在每次运行时都将被重新编译
- 调用存储过程
语句:
[[exec [ute]]
{[返回状态=]
{存储过程名}}] [@参数=] {值|@变量[output]|[default]}] [,···n]
[with recomplie]
SQL server存储过程调用时,参数必须为常数或变量,但不能是表达式或函数
with recomplie:在运行存储过程前对存储过程进行重新编译
- 存储过程的修改和删除
删除
drop procedure {过程名} [,···n]
修改
alert procedure ···同创建存储过程
例:
参数为班号和课程号 ,通过存储过程的输出(output)参数获得指定班级和课程的及格和不及格人数
create procedure getpassnum
@EleId char(6),@ClassId char(6),@PassNum integer output,@NotPassNum integer output
as
select @PassNum=COUNT(*)
from Student_Elevtive a join Student b on a.StdId=b.StdId
where a.EleId=@EleId and b.ClassId=@ClassId and Grade>=60
select @NotPassNum=COUNT(*)
from Student_Elevtive a join Student b on a.StdId=b.StdId
where a.EleId=@EleId and b.ClassId=@ClassId and Grade<60
return
调用
declare @PassN int,@NotPassN int
execute getpassnum 'ele001','0901',@PassN output,@NotPassN output
select @PassN,@NotPassN