SQL SERVER 之存储过程(创建、修改、删除、执行)

mac2025-09-07  6

我就不写一堆原理,书上基本原理肯定是有的。直接用代码,简单粗暴。

创建、修改存储过程格式:

CREATE / ALTER PROCEDURE procedure_name @parameter data_type,--输入参数 @parameter data_type OUTPUT --输出参数 AS Begin SQL 语句 End

例如:

/*创建存储过程*/ create procedure GetStudentAvgAndCount @StuClass nvarchar(30), @StuSex nvarchar(10), @StuCount int output as begin if(@StuClass is not null and @StuClass <> '') begin select st.Sclass, sc.Scourse, avg(sc.Sscore) courseAvg from tb_student st,tb_score sc where st.Sclass = @StuClass and st.Sno = sc.Sno group by st.Sclass,sc.Scourse set @StuCount = (select count(*) from tb_student st where st.Sclass = @StuClass and st.Ssex = @StuSex) end else begin set @StuCount = 0 end end

删除存储过程格式:

DROP PROCEDURE procedure_name

例如:

DORP PROCEDURE GetStudentAvgAndCount

执行存储过程

/*执行存储过程*/ declare @StuMaleCount int exec GetStudentAvgAndCount '一班','男', @StuMaleCount output select @StuMaleCount StuMaleCount

运行结果

最新回复(0)