2017-10-16 76 views
0

如果课堂容量不满,我需要为学生注册课程。PL/SQL将学生录入课堂

学生:

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级的学生吗?

回答

1

我相信,你想要做的是什么 -

  1. 伯爵参加了一个特别班的学生人数。
  2. 如果学生人数少于可以注册的最大值。添加一个学生。

    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; 
    /
    
+0

感谢您回复!现在它变得更有意义。但是,我收到这样的错误:第19行和第21行的“语句忽略”和“子程序或光标'PUT'引用超出了范围'。 – Lizzie

+0

@Lizzie - 将'put.line'更改为'put_line'它现在应该工作 –

+0

哇,我很傻,谢谢!这让我明白了很多。 – Lizzie

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; 
    /