Skip to content

Commit

Permalink
docs(sql): 12.存储过程
Browse files Browse the repository at this point in the history
  • Loading branch information
Leetfs committed Oct 22, 2024
1 parent d6d0d83 commit d253354
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/sql/12/1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: 创建存储过程
author: Lee
---

### 例:创建一个存储过程 `Up_Course_Info`,查询某门课程的选修情况,包括学号姓名课程名字成绩

```sql
CREATE PROCEDURE Up_Course_Info
@CourseName VARCHAR(30) -- 输入参数:课程名称
AS
BEGIN
-- 查询学生的学号、姓名、课程名称和成绩
SELECT Student.StudentID, Student.StudentName, Course.CourseName, SelectCourse.Score
FROM Student
INNER JOIN SelectCourse ON SelectCourse.StudentID = Student.StudentID
INNER JOIN Course ON SelectCourse.CourseID = Course.CourseID
WHERE Course.CourseName = @CourseName;
END
GO
```

## 创建带输出参数的存储过程

输出:`output`

### 创建一个存储过程 `Up_Course_Count`,获取某门课程的选修情况

```sql
CREATE PROCEDURE Up_Course_Count
@CourseName NVARCHAR(100), -- 输入参数: 课程名称
@CourseCount INT OUTPUT -- 输出参数: 选修该课程的学生数
AS
BEGIN
-- 计算选修该课程的学生数
SELECT @CourseCount = COUNT(*)
FROM Course
INNER JOIN SelectCourse ON SelectCourse.CourseID = Course.CourseID
WHERE Course.CourseName = @CourseName;
END;
GO
-- 声明存储输出参数的变量
DECLARE @cntstudent INT;
-- 执行存储过程并获取选修学生数
EXEC Up_Course_Count '课程名', @cntstudent OUTPUT;
-- 打印输出结果
PRINT '选修该课程的学生数为: ' + CAST(@cntstudent AS NVARCHAR);
```
7 changes: 7 additions & 0 deletions docs/sql/12/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: 12.存储过程
index: false
author: Lee
---

电脑端在左侧-侧边栏查看文档,手机端点击左上角`菜单`

0 comments on commit d253354

Please sign in to comment.