Save calculation result to a table in procedure : Procedure Definition « Stored Procedure Function « Oracle PL / SQL






Save calculation result to a table in procedure

    
SQL>
SQL> CREATE TABLE lecturer (
  2    id               NUMBER(5) PRIMARY KEY,
  3    first_name       VARCHAR2(20),
  4    last_name        VARCHAR2(20),
  5    major            VARCHAR2(30),
  6    current_credits  NUMBER(3)
  7    );

Table created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10001, 'Scott', 'Lawson','Computer Science', 11);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major, current_credits)
  2                VALUES (10002, 'Mar', 'Wells','History', 4);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10003, 'Jone', 'Bliss','Computer Science', 8);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10004, 'Man', 'Kyte','Economics', 8);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10005, 'Pat', 'Poll','History', 4);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10006, 'Tim', 'Viper','History', 4);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10007, 'Barbara', 'Blues','Economics', 7);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10008, 'David', 'Large','Music', 4);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10009, 'Chris', 'Elegant','Nutrition', 8);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10010, 'Rose', 'Bond','Music', 7);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10011, 'Rita', 'Johnson','Nutrition', 8);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10012, 'Sharon', 'Clear','Computer Science', 3);

1 row created.

SQL>
SQL>
SQL> CREATE TABLE myStudent (
  2    student_id NUMBER(5) NOT NULL,
  3    department CHAR(3)   NOT NULL,
  4    course     NUMBER(3) NOT NULL,
  5    grade      CHAR(1)
  6    );

Table created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10000, 'CS', 102, 'A');

1 row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10002, 'CS', 102, 'B');

1 row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10003, 'CS', 102, 'C');

1 row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10000, 'HIS', 101, 'A');

1 row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10001, 'HIS', 101, 'B');

1 row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10002, 'HIS', 101, 'B');

1 row created.

SQL>
SQL>
SQL> CREATE TABLE MyTable (
  2    num_col    NUMBER,
  3    char_col   VARCHAR2(60)
  4    );

Table created.

SQL>
SQL>
SQL> CREATE TABLE session (
  2    department       CHAR(3),
  3    course           NUMBER(3),
  4    description      VARCHAR2(2000),
  5    max_lecturer     NUMBER(3),
  6    current_lecturer NUMBER(3),
  7    num_credits      NUMBER(1),
  8    room_id          NUMBER(5)
  9    );

Table created.

SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ('HIS', 101, 'History 101', 30, 11, 4, 20000);

1 row created.

SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ('HIS', 301, 'History 301', 30, 0, 4, 20004);

1 row created.

SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ('CS', 101, 'Computer Science 101', 50, 0, 4, 20001);

1 row created.

SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ('ECN', 203, 'Economics 203', 15, 0, 3, 20002);

1 row created.

SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ('CS', 102, 'Computer Science 102', 35, 3, 4, 20003);

1 row created.

SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ('MUS', 410, 'Music 410', 5, 4, 3, 20005);

1 row created.

SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ('ECN', 101, 'Economics 101', 50, 0, 4, 20007);

1 row created.

SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ('NUT', 307, 'Nutrition 307', 20, 2, 4, 20008);

1 row created.

SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ('MUS', 100, 'Music 100', 100, 0, 3, NULL);

1 row created.

SQL>
SQL>
SQL> CREATE OR REPLACE FUNCTION CountCredits (
  2    p_ID IN lecturer.ID%TYPE)
  3    RETURN NUMBER AS
  4
  5    v_TotalCredits NUMBER;  -- Total number of credits
  6    v_CourseCredits NUMBER; -- Credits for one course
  7    CURSOR c_RegisteredCourses IS
  8      SELECT department, course
  9        FROM myStudent
 10        WHERE student_id = p_ID;
 11  BEGIN
 12    FOR v_CourseRec IN c_RegisteredCourses LOOP
 13      SELECT num_credits
 14        INTO v_CourseCredits
 15        FROM session
 16        WHERE department = v_CourseRec.department
 17        AND course = v_CourseRec.course;
 18
 19      v_TotalCredits := v_TotalCredits + v_CourseCredits;
 20    END LOOP;
 21
 22    RETURN v_TotalCredits;
 23  END CountCredits;
 24  /

Function created.

SQL>
SQL> show error
No errors.
SQL>
SQL> CREATE OR REPLACE PROCEDURE CreditLoop AS
  2    myLecturerID lecturer.ID%TYPE;
  3    v_Credits   lecturer.current_credits%TYPE;
  4    CURSOR c_lecturer IS
  5      SELECT ID
  6        FROM lecturer;
  7  BEGIN
  8    OPEN c_lecturer;
  9    LOOP
 10      FETCH c_lecturer INTO myLecturerID;
 11      v_Credits := CountCredits(myLecturerID);
 12      INSERT INTO MyTable (num_col, char_col)
 13        VALUES (myLecturerID, 'Credits = ' || TO_CHAR(v_Credits));
 14      EXIT WHEN c_lecturer%NOTFOUND;
 15    END LOOP;
 16    CLOSE c_lecturer;
 17  END CreditLoop;
 18  /

Procedure created.

SQL>
SQL> show error
No errors.
SQL>
SQL>
SQL> drop table lecturer;

Table dropped.

SQL> drop table MyTable;

Table dropped.

SQL> drop table myStudent;

Table dropped.

SQL> drop table session;

Table dropped.

SQL>

   
    
    
  








Related examples in the same category

1.Creat an empty procedure
2.replace a procedure
3.Create a stored procedure and how to call it.
4.Define procedure to insert data
5.Define and call procedure
6.Create procedure with authid
7.Create a stored procedure with authid
8.Mutually exclusive local subprograms.
9.A forward declaration.
10.Inner procedure
11.exception throwed out of the procedure
12.AUTHID clause in a CREATE PROCEDURE statement indicates that this procedure is being created with user's or invoker's rights
13.Increase gift price
14.A local subprogram within a stored procedure
15.Overloaded local procedures: number and varchar2
16.Using all the default values
17.Forward Referencing
18.Mark procedure with authid current_user
19.Reference package variable in a procedure
20.Only manager can change the password