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

实例一:查询缺考人数

sql 复制代码
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

即带输出参数 也带输入参数

-- 查询家庭住址是河南的学生的个数,

sql 复制代码
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,并且自定义及格线

sql 复制代码
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
相关推荐
m0_734949795 小时前
MySQL如何配置定时清理过期备份文件_find命令与保留周期策略
jvm·数据库·python
m0_514520575 小时前
MySQL索引优化后性能没提升_通过EXPLAIN查看索引命中率
jvm·数据库·python
NaMM CHIN5 小时前
sql实战解析-sum()over(partition by xx order by xx)
数据库·sql
不瘦80斤不改名6 小时前
深入浅出 MySQL(一):一文理清 SQL 核心规范与五大分类
数据库·sql·mysql
woniu_buhui_fei6 小时前
MySQL知识整理二
数据库·mysql
Polar__Star7 小时前
如何在 AWS Lambda 中正确使用临时凭证生成 S3 预签名 URL
jvm·数据库·python
Lucifer三思而后行7 小时前
zCloud 中 Oracle 实例状态未知问题记录
数据库·oracle
island13147 小时前
最详细VMware Workstation 17 上安装 Ubuntu 系统
linux·数据库·ubuntu
卢傢蕊7 小时前
MongoDB
数据库·mongodb
m0_743623927 小时前
React 自定义 Hook 的命名规范与调用规则详解
jvm·数据库·python