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

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

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

运行结果

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐