DBMS_SQL package: dynamic 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>
SQL> CREATE OR REPLACE PROCEDURE updateSession(
2 p_Department IN session.department%TYPE,
3 p_NewCredits IN session.num_credits%TYPE,
4 p_RowsUpdated OUT INTEGER) AS
5
6 v_CursorID INTEGER;
7 v_UpdateStmt VARCHAR2(100);
8 BEGIN
9 v_CursorID := DBMS_SQL.OPEN_CURSOR;
10
11 v_UpdateStmt :=
12 'UPDATE session
13 SET num_credits = :nc
14 WHERE department = :dept';
15
16 DBMS_SQL.PARSE(v_CursorID, v_UpdateStmt, DBMS_SQL.NATIVE);
17
18 DBMS_SQL.BIND_VARIABLE(v_CursorID, ':nc', p_NewCredits);
19
20 DBMS_SQL.BIND_VARIABLE_CHAR(v_CursorID, ':dept', p_Department);
21
22 p_RowsUpdated := DBMS_SQL.EXECUTE(v_CursorID);
23
24 DBMS_SQL.CLOSE_CURSOR(v_CursorID);
25 EXCEPTION
26 WHEN OTHERS THEN
27 DBMS_SQL.CLOSE_CURSOR(v_CursorID);
28 RAISE;
29 END updateSession;
30 /
Procedure created.
SQL>
SQL> drop table session;
Table dropped.
SQL>
Related examples in the same category