-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
title: 12.存储过程 | ||
index: false | ||
author: Lee | ||
--- | ||
|
||
电脑端在左侧-侧边栏查看文档,手机端点击左上角`菜单` |