- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
实验:游标
一、实验目的与任务
目的:旨在训练学生使用游标遍历数据。
任务:根据需求,创建游标遍历数据。
任务1: 创建一个存储过程,根据给定的系名,要求使用游标输出该系的所有学生学号及姓名的信息。
答案:
create proc prcGetStuInfoByCursor
@DepName varchar(20)
as
begin try
if exists(select * from Department where DepName = @DepName)
begin
declare curStudent cursor for
select StuID,StuName from Student join Department
on Student.DepID=Department.DepID where DepName=@DepName
open curStudent
declare @StuID char(10)
declare @StuName varchar(20)
Fetch curStudent Into @StuID,@StuName
while(@@Fetch_Status = 0)
begin
print @StuID + + @StuName
Fetch curStudent Into @StuID,@StuName
end
close curStudent
deallocate curStudent
end
end try
begin catch
printerror!
end catch
执行:
exec prcGetStuInfoByCursor computer
任务2:使用游标完成教师工资的生成。
答案:
create proc prcGetTeacherSalary
as
begin try
if exists(select * from Teacher)
begin
declare @TeaID char(10)
declare @TeaTitle varchar(20)
declare @CourseNum int
declare @Wage decimal
declare curTeacherWage cursor for
select TeaID,TeaTitle,CourseNum from Teacher
open curTeacherWage
fetch curTeacherWage into @TeaID,@TeaTitle,@CourseNum
while(@@fetch_Status = 0)
begin
if(@TeaTitle = 讲师)
set @Wage = 1000 + 80 * @CourseNum
else if(@TeaTitle = 副教授)
set @Wage = 1500 + 90 * @CourseNum
else
set @Wage = 2000 + 100 * @CourseNum
insert into Salary values(@TeaID,@Wage)
fetch curTeacherWage into @TeaID,@TeaTitle,@CourseNum
end
close curTeacherWage
deallocate curTeacherWage
end
end try
begin catch
printerror!
end catch
执行:
exec prcGetTeacherSalary
任务3: 有Course表和SC表,使用嵌套游标,游标1先找出Course表中的每个课程号,对于找到的课程号,再使用游标2找出选修该课程的所有学生的学号和成绩。
答案:
alter proc prcGetStuInfoByCursor2
as
begin try
declare @CourseID int
declare curCourse cursor for select CourseID from Course
open curCourse
您可能关注的文档
- Linux系统管理实战教程(Red Hat Enterprise Linux 8CentOS 8)课件 任务19 配置Samba文件共享.pptx
- Linux系统管理实战教程(Red Hat Enterprise Linux 8CentOS 8)课件 任务20 配置DHCP服务.pptx
- Linux系统管理实战教程(Red Hat Enterprise Linux 8CentOS 8)课件 任务6 使用shell.pptx
- Linux系统管理实战教程(Red Hat Enterprise Linux 8CentOS 8)课件 任务21 配置DNS域名解析服务.pptx
- Linux系统管理实战教程(Red Hat Enterprise Linux 8CentOS 8)课件 任务22 配置FTP服务.pptx
- Linux系统管理实战教程(Red Hat Enterprise Linux 8CentOS 8)课件 任务23 用Apache部署静态网站.pptx
- Linux系统管理实战教程(Red Hat Enterprise Linux 8CentOS 8)课件 任务24 部署LAMP环境.pptx
- MySQL数据库系统原理 课件 1. Linux与数据库概述.pptx
- MySQL数据库系统原理 课件 5. 索引原理基础.pptx
- MySQL数据库系统原理 课件 6. 事务处理与锁.pptx
原创力文档


文档评论(0)