Вы находитесь на странице: 1из 2

Dpartement Informatique et Statistique, ICOM, Universit Lumire Lyon 2 M1 Informatique Year 2013-2014 Database programming Labwork #5: Packages

es J. Darmont (http://eric.univ-lyon2.fr/~jdarmont/), 15/07/13

4. Add into package statepacks specification and body a procedure named add that inserts into table DEMO_STATES an new state whose ST code and name are passed as parameters. Test the procedure. 5. Add into package statepacks specification and body a procedure named mod that modifies into table DEMO_STATES the state whose ST code and new name are passed as parameters. Test the procedure. 6. Add into package statepacks specification and body a procedure named del that suppresses from table DEMO_STATES the state whose ST code is passed as parameter. Test the procedure. 7. Add into package statepacks body only a function named statecount that returns the number of states in table DEMO_STATES. Test the function from outside the package. Does it work? 8. Call function statecount within procedure display and output the result as NN state(s) below the existing list of states. Where must the statecount function be defined in the package body? How could the statecount function be qualified?

Exercise #1: Package specification only 1. Define a package named Matrix, whose specification includes the following items: a collection type (TABLE) of integers named TheColumns indexed by a binary integer (INDEX BY BINARY_INTEGER); a collection type (TABLE) of TheColumns named TheRows indexed by a binary integer; a variable m of type TheRows.

2. In an anonymous PL/SQL block: initialize each element Matrix.m(i)(j) with i x j (to keep it small, consider a matrix of 2 rows by 3 columns); no declaration is required; diplay Matrix.m.

Test! Is a global variable defined in a package a good idea? Exercise #2: Package specification and body We want to implement a package to manage table DEMO_STATES (you may copy it from table DARMONT.DEMO_STATES onto your account). The objective is to propose procedures and functions to: display the tables contents under format ST: STATE_NAME; add a state; modify a states name (knowing its ST code); delete a state (knowing its ST code); count states.

1. Define the specification of a package named statepack containing: a record type named statetuple with the fields: code and name bearing the same types than that of table DEMO_STATES attributes; a cursor named statelist that returns a statetuple.

2. Define the body of package statepack by fully defining cursor statelist. Test the whole package creation. 3. Add into package statepacks specification and body a procedure named display (no parameter) that displays all states on screen in the desired format. In the procedures code, use cursor statelist and a local variable of type statetuple. Test the procedure (EXECUTE statepack.display or BEGIN statepack.display; END;).

Database programming Labwork #5

1/2

Database programming Labwork #5

2/2

Solution
-- Ex. #1 create or replace PACKAGE matrix AS TYPE TheColumns IS TABLE OF INTEGER INDEX BY BINARY_INTEGER ; TYPE TheRows IS TABLE OF TheColumns INDEX BY BINARY_INTEGER ; m TheRows; -- Global variables are NEVER a good idea END; -- Test block DECLARE i INTEGER; j INTEGER; output VARCHAR(255); BEGIN -- Matrix initialization FOR i IN 1..2 LOOP FOR j IN 1..3 LOOP Matrix.m(i)(j) := i * j; END LOOP; END LOOP; -- Matrix display FOR i IN 1..2 LOOP output := ''; FOR j IN 1..3 LOOP output := output || Matrix.m(i)(j) || ' '; END LOOP; DBMS_OUTPUT.PUT_LINE(output); END LOOP; END; -- Ex. #2 CREATE OR REPLACE PACKAGE statepack AS -- Specification TYPE statetuple IS RECORD( code demo_states.st%TYPE, name demo_states.state_name%TYPE); CURSOR statelist RETURN statetuple; PROCEDURE display; PROCEDURE add( code demo_states.st%TYPE, name demo_states.state_name%TYPE); PROCEDURE mod( code demo_states.st%TYPE, newname demo_states.state_name%TYPE); PROCEDURE del(code demo_states.st%TYPE); END; -- End package specification / SHOW ERRORS

CREATE OR REPLACE PACKAGE BODY statepack AS -- Body CURSOR statelist RETURN statetuple IS SELECT * FROM demo_states; FUNCTION statecount RETURN INTEGER IS -- Not present in specification => private c INTEGER; BEGIN SELECT COUNT(*) INTO c FROM demo_states; RETURN c; END; PROCEDURE display IS s statetuple; BEGIN FOR s IN statelist LOOP DBMS_OUTPUT.PUT_LINE(s.code || ': ' || s.name); END LOOP; DBMS_OUTPUT.PUT_LINE(statecount || ' state(s)'); END; PROCEDURE add( code demo_states.st%TYPE, name demo_states.state_name%TYPE) IS BEGIN INSERT INTO demo_states VALUES(code, name); END; PROCEDURE mod( code demo_states.st%TYPE, newname demo_states.state_name%TYPE) IS BEGIN UPDATE demo_states SET state_name = newname WHERE st = code; END; PROCEDURE del(code demo_states.st%TYPE) IS BEGIN DELETE FROM demo_states WHERE st = code; END; END; -- End package body / SHOW ERRORS -- Tests EXECUTE statepack.display EXECUTE statepack.add('ZZ', 'test') EXECUTE statepack.display EXECUTE statepack.mod('ZZ', 'longer test') EXECUTE statepack.display EXECUTE statepack.del('ZZ') EXECUTE statepack.display EXECUTE DBMS_OUTPUT.PUT_LINE(statepack.statecount) -- doesn't work EXECUTE statepack.display

Database programming Labwork #5

3/2

Database programming Labwork #5

4/2

Вам также может понравиться