创建 PLSQL 代码来打印 10 名学生的成绩

Create PLSQL code to print grades of 10 students

'下面是PLSQL打印学生成绩的程序。程序成功执行但无法打印成绩。任何 suggestions/corrections?

第 1 步

CREATE TABLE STUDENT(
  ROLL_NO   NUMBER,
  NAME      VARCHAR2(100),
  SECTION   NUMBER,
  CLASS     VARCHAR2(100),
  ORACLE    NUMBER,
  Dev_2000  NUMBER
);

INSERT INTO STUDENT (ROLL_NO, NAME, SECTION, CLASS, ORACLE, DEV_2000) values (1, 'AVINASH', 9025, 'STUDENT', 75, 85)
INSERT INTO STUDENT (ROLL_NO, NAME, SECTION, CLASS, ORACLE, DEV_2000) values (2, 'AKASH', 9025, 'WORKING', 45, 85);
INSERT INTO STUDENT (ROLL_NO, NAME, SECTION, CLASS, ORACLE, DEV_2000) values (3, 'ASHISH', 9025, 'WORKING', 48, 67)
INSERT INTO STUDENT (ROLL_NO, NAME, SECTION, CLASS, ORACLE, DEV_2000) values (4, 'AJAY', 9025, 'WORKING', 84, 56)
INSERT INTO STUDENT (ROLL_NO, NAME, SECTION, CLASS, ORACLE, DEV_2000) values (5, 'AKANSHA', 9025, 'WORKING', 62, 78)
INSERT INTO STUDENT (ROLL_NO, NAME, SECTION, CLASS, ORACLE, DEV_2000) values (6, 'AKSHAY', 9025, 'STUDENT', 75, 85)
INSERT INTO STUDENT (ROLL_NO, NAME, SECTION, CLASS, ORACLE, DEV_2000) values (7, 'ATHARVA', 9025, 'WORKING', 45, 85)
INSERT INTO STUDENT (ROLL_NO, NAME, SECTION, CLASS, ORACLE, DEV_2000) values (8, 'RAHUL', 9025, 'WORKING', 48, 67)
INSERT INTO STUDENT (ROLL_NO, NAME, SECTION, CLASS, ORACLE, DEV_2000) values (9, 'VYANKATESH', 9025, 'WORKING', 84, 56)
INSERT INTO STUDENT (ROLL_NO, NAME, SECTION, CLASS, ORACLE, DEV_2000) values (10, 'RUSHIKESH', 9025, 'STUDENT', 62, 78)

第 2 步

CREATE TABLE Dev_2000_result ( ROll_NO Number (4), TOTAL Number (3), PERCENT Number (5,2), GRADE Varchar2 (10) )

第 3 步

DECLARE 
    v_student student%rowtype;
    v_result dev_2000_result%rowtype;
    grade varchar2(10);

    CURSOR c1 IS
        SELECT *
        FROM student;
BEGIN
    FOR v_student IN c1 LOOP 
        IF v_student.class='Working' THEN
            IF v_student.Dev_2000 <50 THEN 
                grade:='FAIL';
            ELSE grade:='PASS';
            END IF;
        ELSIF v_student.class='Student' THEN 
            IF v_student.Dev_2000 >=80 THEN grade:='HONOURS';
                elsif v_student.Dev_2000 >=60 THEN grade:='A';
                elsif v_student.Dev_2000 >=50 THEN grade:='B';
                elsif v_student.Dev_2000 >=40 THEN grade:='C';
                ELSE grade:='B';
            END IF;
        END IF; 

        INSERT INTO Dev_2000_result
        VALUES(v_student.Roll_no,
               v_student.Oracle,
               v_student.Dev_2000,
               grade);
    END LOOP;
END;
/

第 4 步

Execute dev_2000_result;

问题是您将 table 中的大写 class 值与 PL/SQL 中的 non-uppercase 常量进行比较 以下代码应该可以工作

DECLARE
    v_student student%rowtype;
    v_result dev_2000_result%rowtype;
    grade varchar2(10);
    
    CURSOR c1 IS
    SELECT *
    FROM STUDENT;
BEGIN
    FOR v_student IN c1 LOOP
        IF v_student.class='WORKING' THEN
            IF v_student.Dev_2000 <50 THEN grade:='FAIL'; ELSE grade:='PASS'; END IF;
        elsif v_student.class='STUDENT' THEN
            IF v_student.Dev_2000 >=80 THEN grade:='HONOURS';
            elsif v_student.Dev_2000 >=60 THEN grade:='A';
            elsif v_student.Dev_2000 >=50 THEN grade:='B';
            elsif v_student.Dev_2000 >=40 THEN grade:='C';
            ELSE grade:='B';
            END IF;
        END IF;
        
        INSERT INTO Dev_2000_result
        VALUES(v_student.Roll_no, v_student.Oracle, v_student.Dev_2000, grade);
    END LOOP;
END;
/

此外,您不需要 PL/SQL。这可以使用普通 SQL

来实现
insert into dev_2000_result
select roll_no, oracle, dev_2000, decode(class,
        'WORKING', (case when dev_2000 < 50 then 'FAIL' else 'PASS' end),
        'STUDENT', (case when dev_2000 >= 80 then 'HONOURS' when dev_2000 >= 60 then 'A' when dev_2000 >= 50 then 'B' when dev_2000 >= 40 then 'C' else 'B' end)
    ) grade
from STUDENT;

fiddle