数据库语句之存储过程

  • 创建存储过程

语句:

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

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值