黑笔是我写的,红笔是我在网上找的答案。我俩参考表设置的不一样,我设置反了!
他这个语句好像也不是标准SQL,应该是MYSQL还是Oracle吧。希望老师解答~
(1)统计离散数学成绩分布
CREATE TABLE SCC( Grades CHAR(5),
Num INT
) --存人数
INSERT INTO SCC(Grades,Num)
VALUES('>90',0);
INSERT INTO SCC(Grades,Num)
VALUES('>80',0);
INSERT INTO SCC(Grades,Num)
VALUES('>70',0);
INSERT INTO SCC(Grades,Num)
VALUES('>60',0);
INSERT INTO SCC(Grades,Num)
VALUES('<60',0); --初始化
IF (exists (select * from sys.objects where name = 'count_dispersemath'))
DROP PROCEDURE count_dispersemath0
GO
CREATE PROCEDURE count_dispersemath --定义存储过程
AS
BEGIN
DECLARE @beyond90 INT, --定义大于90分的人数
@beyond80 INT, --定义大于80分的人数
@beyond70 INT, --定义大于70分的人数
@beyond60 INT, --定义大于60分的人数
@below60 INT; --定义小于60分的人数
SELECT @beyond90=COUNT(*)
FROM SC
WHERE Grade>90 AND Cno='2' --离散数学的号是2,我直接用的,按理说应该把这个条件变成查询或者增加一个变量赋值为查询结果,但是我是管理员我知道它是2我不查!!
SELECT @beyond80=COUNT(*)
FROM SC
WHERE Grade>80 AND Cno='2'
SELECT @beyond70=COUNT(*)
FROM SC
WHERE Grade>70 AND Cno='2'
SELECT @beyond60=COUNT(*)
FROM SC
WHERE Grade>60 AND Cno='2'
SELECT @below60=COUNT(*)
FROM SC
WHERE Grade<60 AND Cno='2'
UPDATE SCC
SET Num=@beyond90
WHERE Grades='>90'
UPDATE SCC
SET Num=@beyond80
WHERE Grades='>80'
UPDATE SCC
SET Num=@beyond70
WHERE Grades='>70'
UPDATE SCC
SET Num=@beyond60
WHERE Grades='>60'
UPDATE SCC
SET Num=@below60
WHERE Grades='<60'
END;
EXEC count_dispersemath --执行
(2)统计任意一门平均成绩
IF (exists (select * from sys.objects where name = 'GRADEAVG'))
DROP PROCEDURE GRADEAVG
GO
CREATE PROCEDURE GRADEAVG --定义
@cno char(2)=null --参数
AS
BEGIN
SELECT DISTINCT Cname,avg(Grade) AS GradeAVG
FROM SC,Course
WHERE SC.Cno=Course.Cno AND SC.Cno=@cno
GROUP BY Cname; --求这门课的平均成绩
END
EXEC GRADEAVG
@cno=3
EXEC GRADEAVG
@cno=2 --执行
(3)百分制改等级制
ALTER TABLE SC ADD CHARGEMARK CHAR(4) --加一列
GO
CREATE PROCEDURE chargemark
AS
BEGIN
UPDATE SC
SET CHARGEMARK='A'
WHERE Grade>90
UPDATE SC
SET CHARGEMARK='B'
WHERE Grade BETWEEN 80 AND 90
UPDATE SC
SET CHARGEMARK='C'
WHERE Grade BETWEEN 70 AND 80
UPDATE SC
SET CHARGEMARK='D'
WHERE Grade BETWEEN 60 AND 70
UPDATE SC
SET CHARGEMARK='E'
WHERE Grade<60
SELECT * --学生选课成绩改为等级制
FROM Course,SC
WHERE SC.Cno=Course.Cno;
END
EXEC chargemark
盘算了下我的小本本的剩余量,我还是在电脑上敲吧!看起来一堆英文字母挺唬人的,实际上特别简单。写上一个作业的时候只是会读了,消化了一下,再写完这次作业我觉得我可以了!!
感觉就跟C语言里的函数差不多,
CREATE PROCEDURE相当于创建函数,下边@xxx是参数变量,AS BEGIN…END里边写过程条件,最后执行。还有就是把报错的代码改对太开心啦!