The procedure above contains two IN parameters whose values are used by the dynamic SQL statement, and six OUT parameters that hold date returned by the SELECT statement. Once created, the procedure can be tested with the following PL/SQL block: SET SERVEROUTPUT ON DECLARE v_table_name VARCHAR2(20) := '&sv_table_name'; v_id NUMBER := &sv_id; v_first_name VARCHAR2(25); v_last_name VARCHAR2(25); v_street VARCHAR2(50); v_city VARCHAR2(25); v_state VARCHAR2(2); v_zip VARCHAR2(5); BEGIN get_name_address (v_table_name, v_id, v_first_name, v_last_name, v_street, v_city, v_state, v_zip); DBMS_OUTPUT.PUT_LINE ('First Name: 'v_first_name); DBMS_OUTPUT.PUT_LINE ('Last Name: 'v_last_name); DBMS_OUTPUT.PUT_LINE ('Street: 'v_street); DBMS_OUTPUT.PUT_LINE ('City: 'v_city); DBMS_OUTPUT.PUT_LINE ('State: 'v_state); DBMS_OUTPUT.PUT_LINE ('Zip Code: 'v_zip); END; When run, this script produces the following output (the first run is against the STUDENT table, and the second run is against the INSTRUCTOR table): Enter value for sv_table_name: student old 2: v_table_name VARCHAR2(20) := '&sv_table_name'; new 2: v_table_name VARCHAR2(20) := 'student'; Enter value for sv_id: 105 old 3: v_id NUMBER := &sv_id; new 3: v_id NUMBER := 105; First Name: Angel Last Name: Moskowitz Street: 320 John St. City: Ft. Lee State: NJ Zip Code: 07024 PL/SQL procedure successfully completed. Enter value for sv_table_name: instructor old 2: v_table_name VARCHAR2(20) := '&sv_table_name'; new 2: v_table_name VARCHAR2(20) := 'instructor'; Enter value for sv_id: 105 old 3: v_id NUMBER := &sv_id; new 3: v_id NUMBER := 105; First Name: Anita Last Name: Morris Street: 34 Maiden Lane City: New York State: NY Zip Code: 10015 PL/SQL procedure successfully completed.
In the package specification created above, you declare a user-defined record type. This record type is used by the procedure for its OUT parameter, name_addr_rec . Once the package is created, its procedure can be tested with the following PL/SQL block (changes are shown in bold): SET SERVEROUTPUT ON DECLARE v_table_name VARCHAR2(20) := '&sv_table_name'; v_id NUMBER := &sv_id; name_addr_rec DYNAMIC_SQL_PKG.NAME_ADDR_REC_TYPE; BEGIN dynamic_sql_pkg.get_name_address (v_table_name, v_id, name_addr_rec); DBMS_OUTPUT.PUT_LINE ('First Name: 'name_addr_rec.first_name); DBMS_OUTPUT.PUT_LINE ('Last Name: 'name_addr_rec.last_name); DBMS_OUTPUT.PUT_LINE ('Street: 'name_addr_rec.street); DBMS_OUTPUT.PUT_LINE ('City: 'name_addr_rec.city); DBMS_OUTPUT.PUT_LINE ('State: 'name_addr_rec.state); DBMS_OUTPUT.PUT_LINE ('Zip Code: 'name_addr_rec.zip); END; Notice that instead of declaring six variables , you declare one variable of the user-defined record type, name_addr_rec_type . Because this record type has been defined in the package DYNAMIC_SQL_PKG, the name of the record type is prefixed by the name of the package. Similarly, the name of package has been added to the procedure call statement. When run, this script produces the output shown below (the first output is against the STUDENT table, and the second output is against the INSTRUCTOR table): Enter value for sv_table_name: student old 2: v_table_name VARCHAR2(20) := '&sv_table_name'; new 2: v_table_name VARCHAR2(20) := 'student'; Enter value for sv_id: 105 old 3: v_id NUMBER := &sv_id; new 3: v_id NUMBER := 105; First Name: Angel Last Name: Moskowitz Street: 320 John St. City: Ft. Lee State: NJ Zip Code: 07024 PL/SQL procedure successfully completed. Enter value for sv_table_name: instructor old 2: v_table_name VARCHAR2(20) := '&sv_table_name'; new 2: v_table_name VARCHAR2(20) := 'instructor'; Enter value for sv_id: 105 old 3: v_id NUMBER := &sv_id; new 3: v_id NUMBER := 105; First Name: Anita Last Name: Morris Street: 34 Maiden Lane City: New York State: NY Zip Code: 10015 PL/SQL procedure successfully completed. |