更新时间:2022-08-13 16:17:35
存储过程是一组预编译的SQL语句,它可以包含数据操纵语句、变量、逻辑控制语句等。
存储过程允许带参数:
存储过程的优点:
我们可以使用create procedure命令创建存储过程。
create procedure calcAge ( @birthday datetime, --输入参数 @age int output --输出参数,参数后面加 output ) as begin --begin...end 语句块不是必须的(即使是多条语句) declare @now datetime set @now=getdate() set @age=YEAR(@now)-YEAR(@birthday) --为输出参数赋值,不需要return end
输入参数带默认值:
create procedure calcAge ( @birthday datetime = '2012-1-1', --输入参数,带默认值,调用的时候可以不指定 @age int output --输出参数,参数后面加 output ) as begin --begin...end 语句块不是必须的(即使是多条语句) declare @now datetime set @now=getdate() set @age=YEAR(@now)-YEAR(@birthday) --为输出参数赋值,不需要return end
我们新定义的存储过程有输出参数,调用的时候也需要指定参数为output
declare @age int execute calcAge '2012-1-1', @age output --标记参数@age为output print @age
调用存储过程时,默认情况下指定的参数是按照定义的数序指定的,我们也可以显示的指定:
declare @myAge int execute calcAge @age=@myAge output --显示指定参数@age print @myAge
使用alter procedure命令修改存储过程,例如下面的伪代码:
alter procedure calcAge ( @birthday datetime, @age int output ) as begin -- 这里是你的逻辑 end
使用drop procedure命令删除存储过程:
drop procedure calcAge 本文转自齐师傅博客园博客,原文链接:http://www.cnblogs.com/youring2/p/4917333.html,如需转载请自行联系原作者