使用WHILE 语句求1~100之和。 2.使用学籍管理数据库编写以下程序。 1)在Student 表中查找名字为“章会”的学生。如果存在,则显示该学生的信息;否则显示“查无此人”。 2)查询有无选修100200号课程的记录。如果有,则显示“有”,并查询选修100200号课程的人数。 3)查询20192602 班级的学生信息,要求列出的字段为:本班级学生的学号、姓名、性别、出生日期、住址。 4)使用学籍管理数据库,定义一个游标Student_Delete,删除 Student 表中的第一行数据。 5)使用学籍管理数据库,定义一个游标Student Display,将所有学生的姓名、家庭地址显示出来
时间: 2023-11-27 13:53:40 浏览: 97
1. 求1~100之和的程序:
```
DECLARE
sum NUMBER := 0;
i NUMBER := 1;
BEGIN
WHILE i <= 100 LOOP
sum := sum + i;
i := i + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('1~100之和为:'|| sum);
END;
```
2. 学籍管理数据库的程序:
```
-- 1)在Student表中查找名字为“章会”的学生
DECLARE
v_id Student.s_id%TYPE;
v_name Student.s_name%TYPE;
v_gender Student.s_gender%TYPE;
v_birthday Student.s_birthday%TYPE;
v_address Student.s_address%TYPE;
BEGIN
SELECT s_id, s_name, s_gender, s_birthday, s_address
INTO v_id, v_name, v_gender, v_birthday, v_address
FROM Student
WHERE s_name = '章会';
DBMS_OUTPUT.PUT_LINE('学号:' || v_id || ',姓名:' || v_name || ',性别:' || v_gender || ',出生日期:' || v_birthday || ',住址:' || v_address);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('查无此人');
END;
-- 2)查询有无选修100200号课程的记录
DECLARE
v_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO v_count
FROM SC
WHERE c_id = '100200';
IF v_count > 0 THEN
DBMS_OUTPUT.PUT_LINE('有');
SELECT COUNT(*)
INTO v_count
FROM SC
WHERE c_id = '100200';
DBMS_OUTPUT.PUT_LINE('选修100200号课程的人数为:' || v_count);
ELSE
DBMS_OUTPUT.PUT_LINE('无');
END IF;
END;
-- 3)查询20192602班级的学生信息
DECLARE
CURSOR c_student IS
SELECT s_id, s_name, s_gender, s_birthday, s_address
FROM Student
WHERE s_class = '20192602';
v_id Student.s_id%TYPE;
v_name Student.s_name%TYPE;
v_gender Student.s_gender%TYPE;
v_birthday Student.s_birthday%TYPE;
v_address Student.s_address%TYPE;
BEGIN
OPEN c_student;
LOOP
FETCH c_student INTO v_id, v_name, v_gender, v_birthday, v_address;
EXIT WHEN c_student%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('学号:' || v_id || ',姓名:' || v_name || ',性别:' || v_gender || ',出生日期:' || v_birthday || ',住址:' || v_address);
END LOOP;
CLOSE c_student;
END;
-- 4)删除Student表中的第一行数据
DECLARE
CURSOR c_student IS
SELECT s_id
FROM Student
WHERE ROWNUM = 1;
v_id Student.s_id%TYPE;
BEGIN
OPEN c_student;
FETCH c_student INTO v_id;
CLOSE c_student;
DELETE FROM Student
WHERE s_id = v_id;
END;
-- 5)将所有学生的姓名、家庭地址显示出来
DECLARE
CURSOR c_student IS
SELECT s_name, s_home_address
FROM Student;
v_name Student.s_name%TYPE;
v_address Student.s_home_address%TYPE;
BEGIN
OPEN c_student;
LOOP
FETCH c_student INTO v_name, v_address;
EXIT WHEN c_student%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('姓名:' || v_name || ',家庭地址:' || v_address);
END LOOP;
CLOSE c_student;
END;
```
阅读全文