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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
USE Student GO CREATE FUNCTION dbo.Sdept_select_amount(@sdept VARCHAR(20),@cname CHAR(40)) RETURNS INT BEGIN DECLARE @tot INT SELECT @tot=COUNT(*) FROM Student JOIN dbo.SC S on Student.Sno = S.Sno JOIN Course C on C.Cno = S.Cno WHERE Cname=@cname AND Sdept=@sdept RETURN @tot end GO
SELECT dbo.Sdept_select_amount('CS','数据库') AS '计算机系选数据库课程的人数'
CREATE FUNCTION dbo.Sdept_average_grade(@sdept VARCHAR(20)) RETURNS TABLE AS RETURN ( SELECT C.Cno, C.Cname, AVG(Grade) AS '平均成绩' FROM Student JOIN dbo.SC S on Student.Sno = S.Sno JOIN Course C on C.Cno = S.Cno WHERE Sdept = @sdept GROUP BY C.Cno, C.Cname ) GO
SELECT * FROM Sdept_average_grade('CS')
CREATE FUNCTION dbo.Student_info(@sno CHAR(9)) RETURNS @Student_info TABLE(Sname CHAR(20),Cname CHAR(40),Grade SMALLINT,Ccredit SMALLINT) AS BEGIN INSERT @Student_info SELECT Sname,Cname,Grade,Ccredit FROM Student JOIN dbo.SC S on Student.Sno = S.Sno JOIN Course C on C.Cno = S.Cno WHERE Student.Sno=@sno RETURN end GO
SELECT* FROM dbo.Student_info ('200515002')
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
EXEC Course_percent '1'
CREATE PROCEDURE Student_name_info @sname CHAR(20) AS DECLARE @cname VARCHAR(40),@grade SMALLINT DECLARE csr1 CURSOR FOR SELECT Cname,Grade FROM SC JOIN Course C on C.Cno = SC.Cno JOIN Student S on S.Sno = SC.Sno WHERE @sname=S.Sname OPEN csr1 FETCH NEXT FROM csr1 INTO @cname,@grade PRINT @sname PRINT '-----------------------' PRINT ' 课程名 成绩' WHILE @@FETCH_STATUS=0 BEGIN PRINT @cname+CAST(@grade AS CHAR(4)) FETCH NEXT FROM csr1 INTO @cname,@grade end PRINT '=======================' CLOSE csr1 DEALLOCATE csr1 GO
EXEC Student_name_info '赵菁菁'
DECLARE @sno CHAR(9),@sname VARCHAR(20),@cno CHAR(4),@cname VARCHAR(40),@ccredit SMALLINT DECLARE csr2_1 CURSOR FOR SELECT Sno,Sname FROM Student OPEN csr2_1 FETCH NEXT FROM csr2_1 INTO @sno,@sname WHILE @@FETCH_STATUS=0 BEGIN PRINT @sno+' '+@sname PRINT '----------------------------------------------------------' PRINT '课程号 课程名 学分' DECLARE csr2_2 CURSOR FOR SELECT C.Cno,Cname,Ccredit FROM SC JOIN Course C on C.Cno = SC.Cno WHERE @sno=Sno OPEN csr2_2 FETCH NEXT FROM csr2_2 INTO @cno,@cname,@ccredit WHILE @@FETCH_STATUS=0 BEGIN PRINT @cno+' '+@cname+CONVERT(CHAR(4),@ccredit) FETCH NEXT FROM csr2_2 INTO @cno,@cname,@ccredit end PRINT '----------------------------------------------------------' CLOSE csr2_2 DEALLOCATE csr2_2 FETCH NEXT FROM csr2_1 INTO @sno,@sname end CLOSE csr2_1 DEALLOCATE csr2_1 GO
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
EXEC Student_grade_update 1 ,1 EXEC Student_grade_update 1,6
CREATE PROCEDURE Student_grade_update_plus3 @cno CHAR(4) AS DECLARE @avg INT,@sno CHAR(9),@grade SMALLINT SELECT @avg=AVG(Grade) FROM SC WHERE Cno=@cno DECLARE csr4 CURSOR FOR SELECT Sno,Grade FROM SC WHERE Cno=@cno OPEN csr4 FETCH NEXT FROM csr4 INTO @sno,@grade WHILE @@FETCH_STATUS=0 BEGIN IF @grade<@avg BEGIN UPDATE SC SET Grade=Grade+3 WHERE @sno=Sno AND @cno=Cno end FETCH NEXT FROM csr4 INTO @sno,@grade end CLOSE csr4 DEALLOCATE csr4 GO EXEC Student_grade_update_plus3 10
CREATE TABLE Changed ( Sno CHAR(9) PRIMARY KEY , Odept VARCHAR(20), Ndept VARCHAR(20), Avggrade SMALLINT, FOREIGN KEY (SNO) REFERENCES Student(Sno) ) CREATE PROCEDURE Student_sdept_trans @sno CHAR(9),@sdept VARCHAR(20) AS DECLARE @avg INT,@odept VARCHAR(20) SELECT @avg=AVG(Grade) FROM SC WHERE Sno=@sno SELECT @odept=Sdept FROM Student WHERE @sno=Sno IF @avg<=95 AND @sdept='CS' BEGIN PRINT '要求该学生的平均成绩必须超过95分' end ELSE BEGIN UPDATE Student SET Sdept=@sdept WHERE Sno=@sno INSERT INTO Changed(SNO, ODEPT, NDEPT, AVGGRADE) VALUES (@sno,@odept,@sdept,@avg) end GO EXEC Student_sdept_trans 200515009 ,'CS'
CREATE PROCEDURE Library_renew @sno CHAR(9),@bno CHAR(9) AS DECLARE @day DATETIME,@datediff INT SELECT @day=GETDATE() SELECT @datediff=DATEDIFF(dd,lend.应还日期,@day) FROM lend WHERE @sno=Sno AND @bno=Bno IF @datediff>=0 BEGIN UPDATE lend SET 应还日期=DATEADD(yyyy-MM-dd,30,应还日期) WHERE @sno=Sno AND @bno=Bno end ELSE IF @datediff<0 BEGIN PRINT '已经超期,只能还书不能续借' END GO
CREATE PROCEDURE Library_return @sno CHAR(9),@bno CHAR(9) AS DECLARE @day DATETIME,@datediff INT SELECT @day=GETDATE() SELECT @datediff=DATEDIFF(dd,lend.应还日期,@day) FROM lend WHERE @sno=Sno AND @bno=Bno IF @datediff<0 BEGIN PRINT '已经超期,请缴纳罚金' INSERT INTO debt(学号,日期,欠款金额) VALUES (@sno,GETDATE(),CONVERT(FLOAT,ABS(@datediff))*0.1) end INSERT INTO return(学号,书号,还书日期) VALUES (@sno,@bno,GETDATE()) DELETE FROM lend WHERE @sno=Sno AND @bno=Bno GO
|