1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
create procedure p1 @cno char(4), @great int output, @good int output, @medium int output, @pass int output, @passper varchar(10) output as declare @sum float select @great=count(*) from sc where cno=@cno and grade between 90 and 100 select @good=count(*) from sc where cno=@cno and grade between 80 and 89 select @medium=count(*) from sc where cno=@cno and grade between 70 and 79 select @pass=count(*) from sc where cno=@cno and grade between 60 and 69 select @sum=count(*) from sc where cno=@cno select @passper = str(cast((@great+@good+@medium+@pass)*100/@sum as numeric(5,2))) declare @gr int,@go int,@me int,@pa int,@pap varchar(10) exec p1 '1' , @gr output, @go output, @me output, @pa output, @pap output print '优秀人数'+str(@gr) print '良好人数'+str(@go) print '中等人数'+str(@me) print '及格人数'+str(@pa) print '及格率'+@pap+'%'
我的: CREATE PROCEDURE Course_percent @cno CHAR(4) AS DECLARE @S INT,@A INT,@B INT,@C INT,@per VARCHAR(10) SELECT @S=COUNT(CASE WHEN Grade BETWEEN 90 AND 100 THEN 1 END), @A=COUNT(CASE WHEN Grade BETWEEN 80 AND 89 THEN 1 END), @B=COUNT(CASE WHEN Grade BETWEEN 70 AND 79 THEN 1 END), @C=COUNT(CASE WHEN Grade BETWEEN 60 AND 69 THEN 1 END), @per=CONCAT(CAST(CONVERT(FLOAT,COUNT(CASE WHEN Grade BETWEEN 60 AND 100 THEN 1 END))/CONVERT(FLOAT,COUNT(DISTINCT Sno))*100.0 AS NUMERIC(10,2)),'%') FROM SC JOIN Course C2 on C2.Cno = SC.Cno WHERE @cno=SC.Cno PRINT '优秀人数:'+CAST(@S AS VARCHAR(10))+' 良好人数:'+CAST(@A AS VARCHAR(10))+' 中等人数:'+CAST(@B AS VARCHAR(10))+' 及格人数:'+CAST(@C AS VARCHAR(10))+' 及格率:'+@per; GO
create procedure ex7 @dept varchar(8),@cno char(4),@n int as declare @avggrade int select @avggrade=(select avg(grade) from sc where cno=@cno) declare c scroll cursor for select student.sno,sname,sc.cno,cname,grade from student,sc,course where student.sno=sc.sno and course.cno=sc.cno and course.cno=@cno and student.sdept=@dept order by grade desc open c declare @sno char(9),@sname nchar(6),@cno1 char(4),@cname varchar(20),@grade int fetch absolute @n from c into @sno,@sname,@cno1,@cname,@grade if @grade>@avggrade print @sno+' '+@sname+' '+@cno1+' '+@cname+' '+cast(@grade as char(4)) else update sc set grade=@avggrade where current of c close c deallocate c
exec ex7 'CS', '1',3
完蛋,某个专业我就没看见 CREATE PROCEDURE Student_grade_update @cno CHAR(4),@rank INT AS DECLARE @avg INT,@sno CHAR(9),@grade SMALLINT SELECT @avg=AVG(Grade) FROM Student JOIN SC S on Student.Sno = S.Sno WHERE @cno=Cno DECLARE csr3 SCROLL CURSOR FOR SELECT Sno,Cno,Grade FROM SC WHERE @cno=Cno ORDER BY Grade DESC OPEN csr3 FETCH ABSOLUTE @rank FROM csr3 INTO @sno,@cno,@grade IF @grade<@avg BEGIN PRINT '成绩低于这门课程平均分' UPDATE SC SET Grade=@avg WHERE @sno=Sno AND @cno=Cno end ELSE BEGIN SELECT S2.Sno,Sname,C.Cno,Cname,Grade FROM Student JOIN SC S2 on Student.Sno = S2.Sno JOIN Course C on C.Cno = S2.Cno WHERE @cno=C.Cno AND @sno=S2.Sno end CLOSE csr3 DEALLOCATE csr3 GO
use 图书管理 create table reader (学号 char(9),姓名 varchar(10),性别 char(2),余额 money) create table lend (学号 char(9),书号 char(20),借书日期 date,应还日期 date,是否续借 varchar(6))
create table debt (学号 char(9),日期 date,欠款金额 money)
create table [return] (学号 char(9),书号 char(20),还书日期 date) create procedure return_book @sno char(9),@bno char(20) as declare @rdate date,@xujie varchar(6),@day int select @rdate=应还日期,@xujie=是否续借 from lend where 学号=@sno and 书号=@bno if (@xujie='续借' and @rdate>=getdate() ) update lend set 应还日期=dateadd(day,30,应还日期) else begin delete from lend where 学号=@sno and 书号=@bno insert into [return] values (@sno,@bno,getdate()) end if(@rdate<getdate()) begin set @day=datediff(day,@rdate,getdate()) insert into debt values(@sno,getdate(),@day*0.1) update reader set 余额=余额-@day*0.1 where 学号=@sno end
exec return_book '201900801','12345678' exec return_book '201900801','112345678' exec return_book '201900802','12345678'
|