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
相关推荐
YMatrix 官方技术社区7 分钟前
YMatrix 高可用详解:3 种镜像策略在节点宕机时表现有何不同?
运维·数据库·数据仓库·ai·数据库开发·数据库架构·ymatrix
嘟嘟w10 分钟前
SQL注入是什么
数据库·sql·oracle
摇滚侠10 分钟前
Redis 零基础到进阶,Redis 主从复制,笔记55-62
数据库·redis·笔记
surtr112 分钟前
数据库基础(数据库原理和应用)
数据库·sql·mysql·oracle·database
张人玉15 分钟前
c# DataSet 类
数据库·c#·dataset
想用offer打牌17 分钟前
数据库大事务有什么危害(面试版)
数据库·后端·架构
廋到被风吹走18 分钟前
【数据库】【Redis】数据结构全景图:命令、场景与避坑指南
数据结构·数据库·redis
Jaising66618 分钟前
Spring 错误使用事务导致数据可见性问题分析
数据库·spring boot
xixingzhe220 分钟前
数据、数据库分类
数据库
松涛和鸣21 分钟前
34、 Linux IPC进程间通信:无名管道(Pipe) 和有名管道(FIFO)
linux·服务器·c语言·网络·数据结构·数据库