--1.在数据库studentcourse中创建存储过程。存储过程名为proc_1, if exists(select name from sysobjects where name='proc_1' and type='p') drop procedure proc_1 go create proc proc_1 @stsno varchar(6) as select s.学号,s.姓名,s.性别,SC.成绩,SC.课程号,c.课程名 from s ,sc,c where s.学号=@stsno and SC.学号=s.学号 and SC.课程号=c.课程号 go execute proc_1 J0401 --2.在数据库studentcourse中创建存储过程,存储过程名为proc_2, if exists (select name from sysobjects where name='proc_2' and type='p') drop procedure proc_2 go create proc proc_2 @cno varchar(10) as select c.课程号,c.课程名,s.学号,s.姓名,s.系, s.性别 from s,c,sc where s.学号=SC.学号 and SC.课程号=c.课程号 and c.课程号=@cno go execute proc_2 c01 --3.在数据库studentcourse中创建触发器,触发器名为trigger_1, if exists (select name from sysobjects where name='trigger_1' and type='tr') drop trigger trigger_1 go create trigger trigger_1 on c after insert,update,delete as begin print ('课程表被修改了') end go update c set c.学分=5 where c.课程号='c03' --4.在数据库studentcourse中创建触发器,触发器名为trigger_2, if exists (select name from sysobjects where name='trigger_2' and type='tr') drop trigger tirgger_2 go create trigger trigger_2 on c after delete as begin declare @cno char(7) select @cno=deleted.课程号 from deleted delete sc where sc.课程号=@cno end go --5.在数据库studentcourse中创建触发器,触发器名为trigger_3 if exists (select name from sysobjects where name='trigger_3' and type='tr') drop trigger trigger_3 go create trigger trigger_3 on c after update as begin update sc set 课程号=(select 课程号 from sc) where 课程号 in(select 课程号 from c) end 6.创建向课程数据表C添加新记录的存储过程cadd。 if exists(select name from sysobjects where name='cadd' and type='p') drop procedure cadd go create procedure cadd @cno char(3),@cname varchar(20), @socre smallint,@prcno char(3), @teacher char(8) as begin insert into C(课程号,课程名,学分,预选课程号,教师)values(@cno, @cname,@socre, @prcno,@teacher) end go exec cadd 'C06','C#',7,'C01','黎明' select * from c 7.创建修z丈课程数据表C扒L录的存储过程cupdate if exists(select name from sysobjects where name='cupdate' and type='p') drop procedure cupdate go create procedure cupdate @cno char(3),@cname varchar(20), @socre smallint,@prcno char(3), @teacher char(8) as begin update c set 课程号=@cno, 课程名=@cname,学分=@socre, 预选课程号=@prcno,教师=@teacher end go 8. 创建存储过程cdeleted,要求删除课程数据表C中指定课 程号的记录。 if exists(select name from sysobjects where name='cdeleted' and type='p') drop procedure cdeleted go create procedure cdeleted @cno char(3) as begin delete from c where c.课程号=@cno end go 9.创建存储过程avgc,查询指定课程的平均成绩, 如果平均成绩大于80分,则返回状态代码1;否则,返回状态代码2。 if exists(select name from sysobjects where name='avgc' and type='p') drop procedure avgc go create procedure avgc @cno char(3),@back smallint output as begin select @back=AVG(SC.成绩) from SC where SC.课程号=@cno if(@back>80) return 1 else return 2 end go exec avgc c01,1 10.显示存储过程cupdate的参数,数据类型. 存储过程源代码和存储过程相关的数据库对象。 sp_help 'cupdate' 11.在数据库studentcourse中建立一个名为LOOK_g的存储 过程,用于检索指定课程不及格的学生的姓名与学号。 if exists(select name from sysobjects where name='LOOK_g' and type='p') drop procedure avgc go create procedure LOOK_g @cno char(6) as begin if exists(select s.姓名,s.学号 from s,SC where s.学号=SC.学号 and SC.成绩<60) select s.姓名,s.学号 from s,SC where s.学号=SC.学号 and SC.成绩<60 else print '没有不及格学生' end 12.将存储过程cupdate的名称改为course_update, 删除LOOK_g存储过程。 sp_rename cupdate,course_update go drop procedure LOOK_g 13. 定义一个存储过程,查询数据库中指定同学各门功课的 成绩。显示时按照课程号顺序显示。 create procedure LOOK @name char(6) as begin select s.姓名,c.课程号,SC.成绩 from SC,s,c where s.学号=SC.学号 and SC.课程号=c.课程号 and s.姓名=@name group by c.课程号,s.姓名,SC.成绩 end go exec lOOK @name='李丽' 14.在学生选课数据库中,建立触发器c1_trigger,当向 学生选课数据表SC中添加一条记录时,要求新记录的课程号值在 课程数据表C中存在,如果不存在,则拒绝向 学生选课数据表SC添加这条记录。 create trigger c1_trigger on sc after insert as begin if(select count(*)from inserted join c on inserted.课程号=c.课程号)=0 begin rollback tran print '插入无效' end end 15.在学生选课数据库中,建立触发器c2_trigger, 修改课程数据表C中的课程号的值,该字段在学生选课数据表 SC中的值也相应修改。 create trigger c2_trigger on c after update as begin update sc set 课程号=(select 课程号 from inserted where 课程号 in(select 课程号 from deleted)) end go 16.在学生选课数据库中,建立触发器c3_trigger,删除课程 数据表C中的一条记录,如果该条记录的课程号值在学生选课数 据表SC中存在,则不允许删除;否则可以删除。 create trigger c3_trigger on c for delete as begin if exists (select * from deleted c where c.课程号 in(select sc.课程号 from sc)) begin raiserror('不可删除',16,1) end end 17.删除c3_trigger触发器。 drop trigger c3_trigger 18. 创建一触发器c4_trigger,如果修改、删除和插入学生选 课数据表SC中的任何数据 ,则将向客户显示一条信息“不得对数据表进行任何修改!” If exists(select name from sysobjects where name='c4_trigger' and type='tr') drop trigger c4_trigger go create trigger c4_trigger on sc for insert,update,delete as begin raiserror('不得对数据表进行任何修改',16,10) end