1.创建一个学生成绩登记表,插入一些记录,编写一个统计不及格学生数的存储过程,要求采用循环语句和条件判断语句实现。 Create table SC ( Sno varchar(30),--学号 Sc varchar(20),--课程号 Score numeric(10,2)—成绩 )
时间: 2023-05-31 19:05:04 浏览: 90
--插入记录
insert into SC values('001','001',80);
insert into SC values('002','001',60);
insert into SC values('003','001',75);
insert into SC values('004','001',45);
insert into SC values('005','001',90);
insert into SC values('006','001',30);
--创建存储过程
CREATE PROCEDURE count_fail
AS
BEGIN
DECLARE @fail_count int;
SET @fail_count = 0;
DECLARE @Sno varchar(30);
DECLARE @Sc varchar(20);
DECLARE @Score numeric(10,2);
DECLARE cur CURSOR FOR
SELECT Sno, Sc, Score
FROM SC;
OPEN cur;
FETCH NEXT FROM cur INTO @Sno, @Sc, @Score;
WHILE @@FETCH_STATUS = 0
BEGIN
IF @Score < 60
SET @fail_count = @fail_count + 1;
FETCH NEXT FROM cur INTO @Sno, @Sc, @Score;
END;
CLOSE cur;
DEALLOCATE cur;
SELECT @fail_count AS '不及格学生数';
END;
--调用存储过程
EXEC count_fail;
阅读全文