西华大学数据库实验报告(六)_第1页
西华大学数据库实验报告(六)_第2页
西华大学数据库实验报告(六)_第3页
西华大学数据库实验报告(六)_第4页
西华大学数据库实验报告(六)_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第6次作业——存储过程一、环境运行SQLServer,已创建名为student数据库、“学生信息”表、“课程”表、“学生成绩”表。(参考实训二)二、实训内容(1)使用createprocedure命令建立名为StudentCourseScore存储过程,该存储过程用于查询“学生选课名称、学分及分数”视图中的数据。写出程序代码。 usestudentgoifexists(selectnamefromsysobjectswherename='pr_StudentCourseScore'andtype='P')dropprocedurepr_StudentCourseScoregocreateprocedurepr_StudentCourseScoreasselecta.课程名称,a.学分,b.分数from学生成绩_蒲强林b,学生课程_蒲强林awherea.课程号=b.课程号goexecpr_StudentCourseScorego运行结果截图:(2)使用带参数的存储过程StudentAge,根据制定的“年龄”,找出与给定“年龄”相等的学生的“学号”和“姓名”。写出程序代码。 usestudentgoifexists(selectnamefromsysobjectswherename='pr_StudentAge'andtype='P')dropprocedurepr_StudentAgegocreateprocedurepr_StudentAge@ageintasselect学号,姓名from学生信息_蒲强林where年龄=@agegoexecpr_StudentAge19go运行结果截图:(3)在上题中设置@count参数,作为输出参数,返回和给定“年龄”相同的学生的总人数。写出程序代码。 usestudentgoifexists(selectnamefromsysobjectswherename='pr_StudentAge'andtype='P')dropprocedurepr_StudentAgegocreateprocedurepr_StudentAge@ageint,@countintoutputasselect@count=count(*)from学生信息_蒲强林where年龄=@agegodeclare@Scountintexecpr_StudentAge20,@Scountoutputprint'年龄为19的学生总数为:'+cast(@Scountaschar(4))go运行结果截图:(4)使用存储过程实现向“学生信息”表插入一条记录的操作。 usestudentgoifexists(selectnamefromsysobjectswherename='pr_InsStuInfo'andtype='P')dropprocedurepr_InsStuInfogocreateprocedurepr_InsStuInfo@学号char(7),@姓名char(20),@性别char(2),@年龄int,@所在系char(15),@flagintoutputasbegin set@flag=1 insertinto学生信息_蒲强林values(@学号,@姓名,@性别,@年龄,@所在系) if@@ERROR<>0 set@flag=0endreturngodeclare@flagintset@flag=0execpr_InsStuInfo'008','张三','男',21,'计算机',@flagoutputif@flag=1print'学生信息添加成功!'elseprint'学生信息添加失败!'go运行结果截图:(5)在“学生信息”表中,修改和所给的“学号”相同的记录,用存储过程实现。 usestudentgoifexists(selectnamefromsysobjects wherename='pr_UpdStuInfo'andtype='P')dropprocedurepr_UpdStuInfogocreateprocedurepr_UpdStuInfo@学号char(7),@姓名char(20),@性别char(2),@年龄int,@所在系char(15),@flagintoutputasset@flag=1ifexists(select*from学生信息_蒲强林where学号=@学号)update学生信息_蒲强林set姓名=@姓名,性别=@性别,年龄=@年龄,所在系=@所在系where学号=@学号elseset@flag=0returngodeclare@flagintset@flag=0execpr_UpdStuInfo'008','张三','男',21,'计算机',@flagoutputif@flag=1print'学生信息修改成功!'elseprint'学生信息修改失败!’godeclare@flagintset@flag=0execpr_UpdStuInfo'009','李四','男',21,'计算机',@flagoutputif@flag=1print'学生信息修改成功!'elseprint'学生信息修改失败!’go运行结果截图:(6)在“学生信息”表中,删除和所给的“学号”相同的记录,用存储过程实现。usestudentgoifexists(selectnamefromsysobjects wherename='pr_DelStuInfo'andtype='P')dropprocedurepr_DelStuInfogocreateprocedurepr_DelStuInfo@学号char(7),@flagintoutputasbeginset@flag=1ifexists(select*from学生信息_蒲强林where学号=@学号)delete学生信息_蒲强林where学号=@学号elseset@flag=0endreturngodeclare@flagint,@学号char(7)set@学号='008'set@flag=0execpr_DelStuInfo@学号,@flagoutputif@flag=1print'该学生信息删除成功!'elseprint'该学生不存在,无法删除!'go运行结果截图:(7)使用存储过程实现向“学生成绩”表插入一条记录的操作。usestudentgoifexists(selectnamefromsysobjects wherename='pr_InsGrade'andtype='P')dropprocedurepr_InsGradegocreateprocedurepr_InsGrade@学号char(7),@课程号char(7),@分数int,@flagintoutputasbeginset@flag=1ifexists(select*from学生课程_蒲强林,学生信息_蒲强林where学号=@学号and课程号=@课程号)insert学生成绩_蒲强林values(@学号,@课程号,@分数)elseset@flag=0endreturn@flaggodeclare@Snochar(7),@Cnochar(7),@分数int,@flagintset@Sno='9056299'set@Cno='0000005'set@分数=80execpr_InsGrade@Sno,@Cno,@分数,@flagoutputif@flag=1print'该学生成绩添加成功!'elseprint'该记录中学号或课程号不存在,无法添加!'go(8)在“学生成绩”表中,删除和所给的“学号”、“课程号”都相同的记录,用存储过程实现。usestudentgoifexists(selectnamefromsysobjects wherename='pr_DelGrade'andtype='P')dropprocedurepr_DelGradegocreateprocedurepr_DelGrade@学号char(7),@课程号char(7),@flagintoutputasbeginset@flag=1ifexists(select*from学生成绩_蒲强林where学号=@学号and课程号=@课程号)delete学生成绩_蒲强林where学号=@学号and课程号=@课程号elseset@flag=0endreturn@flaggodeclare@Snochar(7),@Cnochar(7)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论