PL / SQL将学生注册到班级

时间:2017-10-16 02:06:34

标签: sql oracle plsql oracle10g

如果班级能力不满,我需要让学生加入课堂。

生:

SNUM  SNAME
****  ****
102   Bob
103   Lee
104   Ali

课程:

DEPT  CNUM
****  ****
FIN   300
FIN   330
FIN   400

ClassSchedule:

ClassNum  Instructor  Capacity
********  **********  *******
10510     Larry        3
10210     Bob          5
10401     Sally        10

许可登记:

SNUM  ClassNum
****  ********
102   10510
103   10510
104   10401

我的伪代码:

CREATE OR REPLACE Procedure AddStudent(
  p_snum    students.SNUM%type
  p_ClassNum SchClasses.ClassNum%Type ) 
AS
  p_Capacity_SchClasses number;

BEGIN
  SELECT Count(*) into p_Capacity_ClassSchedue
  FROM SchClasses cs
  JOIN Enrollments e ON cs.ClassNum = e.ClassNum
  GROUP BY sc.CallNum

  IF count(p_ClassNum) <=  p_Capacity_SchClasses THEN
    insert into E values(p_SNUM, p_ClassNum, null);
    dbms_output.put.line('Congrats, you are now enrolled.');
  ELSE
    dbms_output.put.line('Sorry, this class is full.');
  END IF;
END;
/

如何将p_snum和p_classnum计为1名学生?

2 个答案:

答案 0 :(得分:1)

我相信,你想做的是 -

  1. 计算在特定课程中注册的学生人数。
  2. 如果学生人数少于可以注册的MAX。添加一名学生。

    CREATE OR REPLACE Procedure AddStudent(
      p_snum    students.SNUM%type
      p_ClassNum SchClasses.ClassNum%Type ) 
    AS
      p_num_current_enrolled NUMBER := 0;
      p_num_max_capacity NUMBER := 0;
    BEGIN
      -- the below will find the number of students enrolled in the class which you want to add a new student to
      SELECT Count(*) into p_num_current_enrolled
      FROM enrollments
      where ClassNum  = p_ClassNum;
    
      -- Get the max capacity of the class
      SELECT capacity into p_max_capacity
      from ClassSchedule
      where ClassNum = p_ClassNum;
    
    
      IF p_num_current_enrolled <  p_max_capacity THEN
         insert into Enrollments values(p_SNUM, p_ClassNum, null);
         dbms_output.put_line('Congrats, you are now enrolled.');
      ELSE
         dbms_output.put_line('Sorry, this class is full.');
      END IF;
    END;
    /
    

答案 1 :(得分:1)

    CREATE OR REPLACE Procedure AddStudent(
      p_snum    students.SNUM%type
      p_ClassNum SchClasses.ClassNum%Type ) 
    AS
      p_Capacity_SchClasses number;
     v_capcity number;


    BEGIN
    --Get current capacity for class number passed into the procedure
      SELECT Count(*), cs.ClassNum 
        into p_Capacity_ClassSchedule 
      FROM SchClasses cs
      JOIN Enrollments e ON cs.ClassNum = e.ClassNum
     Where cs.ClassNum = p_ClassNum
    --get the total capacity of the class number passed in to procedure
     Select Capacity
       INTO v_capcity
       FROM SchClasses
     WHERE ClassNum = p_ClassNum

--Check If the total capacity is greater than the current capacity 
      IF v_capcity >  p_Capacity_SchClasses THEN
        insert into E values(p_SNUM, p_ClassNum, null);
        dbms_output.put.line('Congrats, you are now enrolled.');
      ELSE
        dbms_output.put.line('Sorry, this class is full.');
      END IF;
    END;
    /