1.创建一个学生成绩登记表,插入一些记录,编写一个统计不及格学生数的存储过程,要求采用循环语句和条件判断语句实现。
时间: 2023-05-31 09:05:25 浏览: 93
存储过程(循环插入数据)
创建学生成绩登记表:
```sql
CREATE TABLE student_scores (
id INT PRIMARY KEY,
name VARCHAR(50),
score INT
);
```
插入记录:
```sql
INSERT INTO student_scores VALUES (1, '张三', 80);
INSERT INTO student_scores VALUES (2, '李四', 60);
INSERT INTO student_scores VALUES (3, '王五', 70);
INSERT INTO student_scores VALUES (4, '赵六', 40);
INSERT INTO student_scores VALUES (5, '钱七', 90);
```
编写统计不及格学生数的存储过程:
```sql
CREATE PROCEDURE count_fail_students()
BEGIN
DECLARE count INT DEFAULT 0;
DECLARE i INT DEFAULT 1;
DECLARE n INT DEFAULT 0;
SELECT COUNT(*) INTO n FROM student_scores;
WHILE i <= n DO
DECLARE score INT;
SELECT score INTO score FROM student_scores WHERE id = i;
IF score < 60 THEN
SET count = count + 1;
END IF;
SET i = i + 1;
END WHILE;
SELECT count;
END;
```
执行存储过程:
```sql
CALL count_fail_students();
```
输出结果为:
```
+-------+
| count |
+-------+
| 2 |
+-------+
```
阅读全文