sqlserver 带输出参数的存储过程的创建与执行

时间:2022-11-13 23:00:53

创建

use StudentManager
go
if exists(select * from sysobjects where name='usp_ScoreQuery4')
drop procedure usp_ScoreQuery4
go
create procedure usp_ScoreQuery4 --创建带参数的存储过程
@AbsentCount int output,--缺考总人数
@FailedCount int output,--不及格总人数
@CSharp int=60,
@DB int=60
as
select Students.StudentId,StudentName,C#=CSharp,DB=SQLServerDB
from Students
inner join ScoreList on Students.StudentId=ScoreList.StudentId
where CSharp<@CSharp or SQLServerDB<@DB --显示结果列表
select @AbsentCount=count(*) from Students
where StudentId not in(select StudentId from ScoreList) --查询缺考总人数
select @FailedCount=count(*) from ScoreList
where CSharp<@CSharp or SQLServerDB<@DB --查询不及格总人数
go

执行

 use StudentManager
go
--调用带参数的存储过程
declare @AbsentCount int,@FailedCount int --首先定义输出参数
exec usp_ScoreQuery4 @AbsentCount output,@FailedCount output
--使用反馈的结果
select 缺考总数=@AbsentCount,不及格总数=@FailedCount