SQL的存储过程 例题

1、创建存储过程P1,查询每个学生的修课门数,要求列出学生学号、姓名及修课门数。

create proc P1

as

select Student.StudentID,StudentName,count(CourseID)选修门数 from Student join Grade

on Grade.StudentID=Student.StudentID

group by Student.StudentID,StudentName

go

2、创建存储过程P2,查询学生的学号、姓名、课程名、成绩,将学生所在系作为输入参数

create proc P2

@departmentname varchar(20)

as

select Student.StudentID,StudentName,CourseName,Grade from Student join Grade

on Grade.StudentID=Student.StudentID join Course

on Course.CourseID=Grade.CourseID join Class

on Class.ClassID=Student.ClassID join Department

on Department.DepartmentID=Class.DepartmentID

where DepartmentName=@departmentname

go

3、创建存储过程P3,查询指定系的男生人数,其中系为输入参数,人数为输出参数。

create proc P3

@departmentname varchar(20),

@num int output

as

select @num=count(StudentID) from Student join Class

on Class.ClassID=Student.ClassID join Department

on Department.DepartmentID=Class.DepartmentID

where DepartmentName=@departmentname and Sex='男'

go

4、创建存储过程P4,删除指定学生的修课记录,其中学号为输入参数。

create proc P4

@sid char(12)

as

 delete  from  Grade where  StudentID=@sid

go

5、创建存储过程P5, 通过输入教师的教师号返回教师性别

create proccP5

@tid char(8),

@sex char(2) output

as

select @sex=Sex from Teacher where TeacherID=@tid

6、创建存储过程P6,通过输入的班级名称能够返回该班级的女生人数。

create proc P6

@cname varchar(20),

@gnum int output

as

select @gnum=count(StudentID)from Student join Class

on Class.ClassID=Student.ClassID

where ClassName=@cname and Sex='女'

go

7、创建存储过程P7,通过输入的班级名称能够返回该班级的平均成绩。

create proc P7

@cname varchar(20),

@avg int output

as

select @avg=avg(Grade) from Grade join Student

on Student.StudentID=Grade.StudentID join Class

on Class.ClassID=Student.ClassID

where ClassName=@cname

go

8、创建存储过程P8,通过输入的教师姓名能够返回该老师的职称

create proc P8

@tname char(12),

@profession char(8) output

as

select @profession=Profession from Teacher where Teachername=@tname

9、创建存储过程P9,通过输入学生的学号与课程的课程号查询成绩

create proc P9

@sid char(12),

@cid char(8),

@grade numeric(5,2) output

as

select @grade=Grade from Grade where CourseID=@cid and StudentID=@sid

go
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值