创建存储过程:
create procedure 过程名
@parameter 参数类型
@parameter 参数类型
as
begin
………
End
执行存储过程:(exec)execute 过程名
带有输入参数的存储过程
找出三个数字中的最大数:
create proc proc_sql6
@num1 int,
@num2 int,
@num3 int
as
begin
declare @max int
if @num1>@num2
set @max = @num1
else set @max = @num2
if @num3 > @max
set @max = @num3
print '3个数中最大的数字是:' + cast(@max as varchar(20))
end
execute proc_sql6 15, 25, 35
带输入和输出参数的存储过程:
显示指定仓库号的职工信息和该仓库号的最大工资和最小工资
create proc proc_sql9
@cangkuhao varchar(50),
@maxgz int output,
@mingz int output
as
begin
select * from 职工 where 仓库号=@cangkuhao
select @maxgz=MAX(工资) from 职工 where 仓库号=@cangkuhao
select @mingz=MIN(工资) from 职工 where 仓库号=@cangkuhao
end
declare @maxgz int, @mingz int
execute proc_sql9 'wh1', @maxgz output, @mingz output
select @maxgz as 职工最大工资, @mingz as 职工最小工资
查看存储过程的功能代码
execute sp_helptext proc_sql9
重命名存储过程名
execute sp_rename 原存储过程名, 新存储过程名
删除存储过程
drop 过程名