前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【SQL Server】存储过程

【SQL Server】存储过程

作者头像
MaybeHC
发布2024-04-23 19:01:43
440
发布2024-04-23 19:01:43
举报
文章被收录于专栏:技术之路技术之路

如何创建存储过程

定义存储过程的语法 CREATE PROC[EDURE] 存过过程名 @参数1 数据类型 = 默认值 OUTPUT, … …, @参数n 数据类型 = 默认值 OUTPUT, AS SQL语句 GO

存储过程的参数

  • 和C#语言的方法一样,参数可选。
  • 参数分为输入参数、输出参数。
  • 输入参数允许有默认值。

存储过程示例

Students表

这里写图片描述
这里写图片描述

ScoreLIst表

这里写图片描述
这里写图片描述

根据所给的CSharp和DB的分数筛选数据 带输入参数的存储过程

代码语言:javascript
复制
use StudentManager
go
if exists(select * from sysobjects where name ='usp_ScoreQuery2')
drop procedure usp_ScoreQuery2
go
--创建带参数的存储过程
create procedure usp_ScoreQuery2
@CSharp int,
@DB int 
as 
    select Students.StudentId,StudentName,CSharp ,SQLServerDB from Students 
    inner join ScoreList on Students.StudentId = ScoreList.StudentId
    where CSharp <@CSharp or SQLServerDB<@DB
go
--调用带参数的存储过程
exec usp_ScoreQuery2 60,65 --按照参数顺序赋值

根据所给的CSharp和DB的分数筛选数据,记录缺考人数和不及格人数 带输入参数和输出参数的存储过程

代码语言:javascript
复制
use StudentManager
go
if exists(select * from sysobjects where name ='usp_ScoreQuery3')
drop procedure usp_ScoreQuery3
go
--创建带参数的存储过程
create procedure usp_ScoreQuery3
--out一般写在输入参数之前
@ASentCount int output,--缺考人数
@FailedCount int output,--不及格总人数
@CSharp int=60,
@DB int=60 
as 
    select Students.StudentId,StudentName,CSharp ,SQLServerDB from Students 
    inner join ScoreList on Students.StudentId = ScoreList.StudentId
    where CSharp <@CSharp or SQLServerDB<@DB

    --查询统计结果
    select @ASentCount=count(*) from Students
    where StudentId not in (Select StudentId from ScoreList) --查询缺考的人数
    select @FailedCount=count(*) from ScoreList
    where CSharp<@CSharp or SQLServerDB<@DB --查询不及格的总人数

go
--调用带输出参数的存储过程
declare @ASentCount int,@FailedCount int --首先定义输出参数
exec usp_ScoreQuery3 @ASentCount output,@FailedCount output
select @ASentCount 缺考人数,@FailedCount 不及格人数
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-04-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 如何创建存储过程
  • 存储过程示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档