预览加载中,请您耐心等待几秒...
1/7
2/7
3/7
4/7
5/7
6/7
7/7
在线预览结束,喜欢就下载吧,查找使用更方便
如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
第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),@flagintoutputasbeginset@flag=1insertinto学生信息_蒲强林values(@学号,@姓名,@性别,@年龄,@所在系)if@@ERROR<>0set@flag=0endreturngodeclare@flagintset@flag=0execpr_InsStuInfo'008','张三','男',21,'计算机',@flagoutputif@flag=1print'学生信息添加成功!'elseprint'学生信息添加失败!'go运行结果截图:(5)在“学生信息”表中,修改和所给的“学号”相同的记录,用存储过程实现。usestudentgoifexists(selectnamefromsysobjectswherename='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学号=@学号elses