可以使用 PL/SQL 编写一个存储过程来实现统计每年的在校人数。以下是一个示例的 PL/SQL 代码:

CREATE OR REPLACE PROCEDURE count_students AS
  CURSOR c_students IS
    SELECT year_of_admission, num_of_students, duration_of_study
    FROM students;
    
  v_year_of_admission NUMBER;
  v_num_of_students NUMBER;
  v_duration_of_study NUMBER;
  v_total_students NUMBER := 0;
BEGIN
  -- 初始化在校人数为0
  v_total_students := 0;
  
  -- 遍历每个学生记录
  FOR student IN c_students LOOP
    v_year_of_admission := student.year_of_admission;
    v_num_of_students := student.num_of_students;
    v_duration_of_study := student.duration_of_study;
    
    -- 根据学制统计在校人数
    IF v_duration_of_study = 3 THEN
      v_total_students := v_total_students + v_num_of_students;
    ELSIF v_duration_of_study = 4 THEN
      v_total_students := v_total_students + (v_num_of_students * 2);
    END IF;
    
    -- 输出每年的在校人数
    DBMS_OUTPUT.PUT_LINE('Year: ' || v_year_of_admission || ', Number of Students: ' || v_total_students);
  END LOOP;
  
  -- 输出总在校人数
  DBMS_OUTPUT.PUT_LINE('Total Number of Students: ' || v_total_students);
END;
/

在上述代码中,students 表存储了学生的入学年份、入学人数和学制。通过使用游标,我们可以遍历每个学生记录,并根据学制统计在校人数。最后,我们输出每年的在校人数和总在校人数。

你可以调用这个存储过程来执行统计:

BEGIN
  count_students;
END;
/

执行上述代码后,你将看到每年的在校人数和总在校人数的输出结果。

PL/SQL 统计每年在校人数 - 示例代码和详解

原文地址: https://www.cveoy.top/t/topic/o6As 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录