T - SQL 输出参数的过程

输出参数的存储过程的定义:执行完存储过程之后得到具体的值

带输入参数、输出参数、默认值参数的定义方式

create procedure 过程名
--    @i1 int output,
--    @i4 int output,
--    @i2 int,
--    @i3 int,
--    @i6 int = 10
--as
--    sql语句
--go

--declare @i7 int,@i8 int
--exec 过程名 @i7 output,@i8 output,@i2 = 13,@i3 = 14,@i6 = 20

实例一:查询缺考人数

use SMDB
go

-- 查询不及格或者及格的人数 以及缺考的人数
create procedure usp_tt3
	-- 输出参数 返回值
	@jigecount int output, -- 及格人数
	@quekaocount int output -- 缺考人数

as
	-- 及格的人数,必须两门成绩都得大于60才能算及格
	select @jigecount = count(*) from
	ScoreList where CSharp>60 and SqlserverDB >60

	-- 缺考人数
	select @quekaocount = count(*) from Students where StudentId not in (select StudentId from ScoreList)
go
declare @jige int,@quekao int

把@jigecount值赋值给变量@jige
    再去调用具有输出参数过程时候,必须在输出参数后面添加output关键字,并且需要先定义变量,
    用定义变量接收输出参数值

exec usp_tt3 @jige output,@quekao output
print @jige
print @quekao
-- 以表格的形式展示及格和缺考人数
select 及格人数 = @jige,缺考人数=@quekao

 即带输出参数 也带输入参数 
-- 查询家庭住址是河南的学生的个数,

use SMDB
go
create procedure usp_gua
	@count int output, -- 输入参数 河南人数
	@address varchar(10) -- 输入参数 住址,
as
	select @count = count(*) from Students where StudentAddress = @address
go
-- 获取河南的人数
declare @c1 int 
exec usp_gua @c1 output,@address = '河南'
print @c1

-- 获取湖北省人数
declare @c2 int
exec usp_gua @c2 output,@address = '湖北'
print @c2

 实例二:存储过程默认参数的定义方式

查询成绩CSharp成绩大于60的个数,默认及格线是60,并且自定义及格线

use SMDB
go
-- private int Add(int a,int b)
create procedure usp_ggd
	@count int output,
	@score int = 70 --score的默认值就是70
as
	select @count = count(*) from ScoreList where CSharp > @score 
go

declare @jigecount int
exec usp_ggd @jigecount output --本次调用没有传输入参数,参数值是默认值60
print @jigecount 

exec usp_ggd @jigecount output,@score = 70
print @jigecount

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值