(11) Subprograms: Stored Functions







About Functions:
  • Although a function can return more than one value, it is not a good programming practice to do so.
  • Functions can accept parameters in three modes: IN, OUT, and IN OUT.
  • It IS NOT good programming practice to build functions with parameter modes other than IN because functions always return a value.
  • Functions having compilation errors will have an invalid status, and these functions cannot be invoked.
  • Host or bind variables CANNOT be referenced in the PL/SQL block of the function.
  • There can be more than one RETURN statement in the executable section of the function, but the function stops executing when it meets the first RETURN statement.

  • HEADING: must have a RETURN clause.
    • (A PROCEDURE SHOULD NOT have a RETURN clause).
    • A function heading can include these options:
    • DETERMINISTIC - tells optimizer to avoid redundant invocations. Asserts that EVERY call to the functions with the same values will lead to the same result.
    • PARALLEL_ENABLE - Make the function safe for use in slave sessions of parallel DML Evaluations.
    • PIPELINED -

  • EXECUTABLE section:
    • EVERY execution path MUST lead to a RETURN statement. (or PL/SQL issue a compile-time warning:.
    • "PLW-05005: subprogram F returns without value at line n."

  • RETURN statement: Is optional and NOT RECOMMENDED in procedures.
    • Procedure: the RETURN statement returns control to the invoker, where execution resumes immediately after the invocation. The RETURN statement cannot specify an expression.
    • Anonymous block: the RETURN statement exits its own block and all enclosing blocks. The RETURN statement cannot specify an expression.

set serveroutput on 
BEGIN
  BEGIN
    DBMS_OUTPUT.PUT_LINE('Inside inner block.');
    RETURN;
    DBMS_OUTPUT.PUT_LINE('Unreachable statement.');
  END;
  DBMS_OUTPUT.PUT_LINE('Inside outer block. Unreachable statement.');
END;















Invoking functions from SQL statements
  1. Function should be stored in the database.
  2. Function should have ONLY parameters of the IN mode.
  3. Return DATA TYPE should be compatible with SQL datatypes.
    • (i.e., BOOLEAN is NOT an SQL type, so a function returning BOOLEAN cannot be invoked from a SQL statement.)
    • (i.e., You cannot use PL/SQL data types, such as BOOLEAN, RECORD, or TABLE for returning values.)
  4. Must use POSITIONAL notation to pass values to the IN parameters.
  5. Function that MODIFY any DB Table CANNOT be used in SELECT statemnts
    • (attempts on the contrary will raise: "ORA-14551: cannot perform a DML operation inside a query")
    • the restriction on a function that it cannot issue any INSERT, UPDATE, and DELETE statements, when called from within a SELECT statement, will not stop you from creating a packaged function that violates this rule. The violation will be detected at run time.
  6. Functions used on DELETE or UPDATE cannot query or modify any database tables modified by that statements.
    • (attempts otherwise will lead to a MUTATING TABLE error)
    • (i.e. ORA-04091: table is mutating, trigger/function may not see it)
  7. Functions called from SQL statements CANNOT use:
    • TCL: transaction control language, such as COMMIT
    • SessionCL: session control language, such as SET ROLE.
    • SysCL: System control language, such as ALTER SYSTEM, and
    • DDL statements, such as CREATE.

Using Pragma RESTRICT_REFERENCES
  • PRAGMA RESTRICT_REFERENCES allow a packaged function to be used in a GROUP BY clause of a SELECT statement.
  • i.e. pragma restrict_references (check_emp, wnds, wnps, rnps, rnds);
  • The PRAGMA RESTRICT_REFERENCES indicates to the compiler that the function:
    • Writes No Database State (WNDS) – The function cannot issue any INSERT, UPDATE, or DELETE.
    • Writes No Package State (WNPS) – The function does not make any changes to the packaged variables.
    • Reads No Package State (RNPS) – The function reads no package constructs.
    • Reads No Database State (RNDS) – The function issues no SELECT statements.

Using pragma RESTRICT_REFERENCES
SQL> create table empsm (name varchar2(20), salary number);

SQL> create or replace package test_restrict_ref 
is
 function check_emp return number;
 pragma restrict_references (check_emp, wnds, wnps, rnps, rnds);
end test_restrict_ref;
/
PACKAGE BODY test_restrict_ref compiled

SQL> create or replace package body test_restrict_ref
is 
  function check_emp
    return number is
   sum_sal number := 0; 
  begin
   select avg(salary) into sum_sal   -- violates rdns
   from employees;
   insert into empsm                 -- violates wdns
   values ('New emp', sum_sal);
   return sum_sal;
  exception
    when no_data_found then
       sum_sal := 0;
       return sum_sal;
  end check_emp;
end;
/
PACKAGE BODY test_restrict_ref compiled
Warning: execution completed with warning


show errors;
3/3            PLS-00452: Subprogram 'CHECK_EMP' violates its associated pragma




Using the RESULT_CACHE option
RESULT_CACHE [OPTION (declaration) | CLAUSE (definition)]
Stores function results in the PL/SQL function result cache

PL/SQL Function Result Cache
  • Is a language-supported and system-managed way to cache the results of PL/SQL functions in a shared global area (SGA).
  • To enable result-caching for a function, use the RESULT_CACHE clause.
  • Each time a result-cached PL/SQL function is invoked with different parameter values, those parameters and their result are stored in the cache.
  • If the system needs more memory, it ages out (deletes) one or more cached results.
  • If changes to any of the data sources (tables and views) queried by the function are committed, the cached result becomes invalid and must be recomputed.
  • Can save significant space and time.
  • The best candidates for result-caching are functions that are invoked frequently but depend on information that changes infrequently or never.

RESULT_CACHE hint (query)
  • RESULT_CACHE hint instructs the database to cache the results of the current query or query fragment in memory and then to use the cached results in future executions of the query or query fragment.
  • The hint is recognized in the top-level query, the subquery_factoring_clause, or FROM clause inline view.
  • The cached results reside in the result cache memory portion of the shared pool.
  • A cached result is automatically invalidated whenever a database object used in its creation is successfully modified.
  • This hint takes precedence over settings of the RESULT_CACHE_MODE initialization parameter.
  • The query is eligible for result caching only if all functions entailed in the query—for example, built-in or user-defined functions or virtual columns—are deterministic.
  • If the query is executed from OCI client and OCI client result cache is enabled, then RESULT_CACHE hint enables client caching for the current query.




















Using the RESULT_CACHE option/clause
The clause needs to be included in the Function declaration
SQL> CREATE OR REPLACE PACKAGE department_pkg IS
 type dept_info_rec is RECORD (
   dept_name departments.department_name%TYPE,
   mgr_name  employees.last_name%type,
   dept_size pls_integer);
 
 -- function declaration
 FUNCTION get_dept_info (dept_id pls_integer)
   RETURN dept_info_rec
   RESULT_CACHE;                   -- result_cache option declared.
END department_pkg;
/
PACKAGE department_pkg compiled.


-- Because get_dept_info depends on the DEPARTMENTS and EMPLOYEES tables, 
-- ANY COMMITTED change to DEPARTMENTS or EMPLOYEES invalidates 
-- all cached results for get_dept_info
SQL> CREATE OR REPLACE PACKAGE BODY department_pkg IS
  -- function definition.
 FUNCTION get_dept_info (dept_id pls_integer)
   return dept_info_rec
   result_cache RELIES_ON (departments, employees) -- identify dependencies
 IS
   rec dept_info_rec; 
 BEGIN
   SELECT department_name into rec.dept_name
   from departments
   where department_id = dept_id;
   
   select e.last_name INTO rec.mgr_name
   from departments d, employees e
   where d.department_id = dept_id
   and d.manager_id = e.employee_id;
   
   select count(*) INTO rec.dept_size
   from employees
   where department_id = dept_id;
   
   return rec;
 END get_dept_info;
END department_pkg;

set serveroutput on
declare
  rec department_pkg.dept_info_rec;
begin
  rec := department_pkg.get_dept_info(10);
  dbms_output.put_line('Department:    '|| rec.dept_name);
  dbms_output.put_line('Manager:       '|| rec.mgr_name);
  dbms_output.put_line('Num Employees: '|| rec.dept_size);
end;
/
anonymous block completed
Department:    Administration
Manager:       Whalen
Num Employees: 1


Restrictions to result-cache functions
  • Cannot be defined in a module that has invoker's rights.
  • Cannot be defined in an anonymous block
  • Cannot be pipelined
  • Does not references dictionary tables, temp tables, sequences or nondeterministic SQL functions.
  • Has no OUT or IN OUT parameters
  • No IN parameter of type: BLOB, CLOB, NCLOB, REF CURSOR, Collection, Object, Record
  • RETURN type cannot be: BLOB, CLOB, NCLOB, REF CURSOR, record or collection with an unsupported return type.

Recommended for result-cached functions:
  • Should not have side effects.
  • Should not depend on session-specific settings
  • should not depend on session-specific application contexts

when does a subprogram has side effects?
If it changes ANYTHING except the values of its own local variables. i.e.
  • changes its own OUT or IN OUT parameter
  • changes a global variable
  • changes a public variable in a package
  • changes a database table
  • changes the database
  • changes the external state (use dbms_output or send email).

What kinds of PL/SQL FUnctions can be called by SQL Statements?
The function must obey these PURITY RULES (control side effects):
  1. Function is invoked from SELECT or parallelized Insert/Update/Delete (I/U/D): Subprogram CANNOT modify any DB tables.
  2. Invoked from I/U/D: subprogram cannot query or modify any DB tables modified by the statement. (ORA-04091 - Mutating Table)
  3. Invoked from SELECT I/U/D: Subprogram CANNOT execute any of these statement types: (except if uses PRAGMA Autonomous_transaction)
    • Transaction Control (i.e. COMMIT)
    • Session Control (i.e. SET ROLE, ALTER SESSION)
    • System control (i.e. ALTER SYSTEM)
    • DDL (i.e. CREATE), which are commited automatically.


Result Cache Bypass
  • Ensures that the user of each session sees his or hers own uncommitted changes
    Put the result cache in bypass mode.
BEGIN 
 DBMS_RESULT_CACHE.Bypass(TRUE);
END;

Result Cache management:
  • Initialization Parameters
    • RESULT_CACHE_MAX_SIZE: Max amount of SGA (bytes)
    • RESULT_CACHE_MAX_RESULT: Max % of the result caceh any single result can use.
  • Package:
    • DBMS_RESULT_CACHE: provides interface allowing DBA to administer the RESULT_CACHE in the SGA.
  • Dynamic performance views:
    • [G]V$RESULT_CACHE_STATISTICS
    • [G]V$RESULT_CACHE_MEMORY
    • [G]V$RESULT_CACHE_OBJECTS
    • [G]V$RESULT_CACHE_DEPENDENCY


How Result-cached functions handle session-specific application contexts?
  • Application Context: Either GLOBAL or session-specific.
  • Is a set of attributes and their values.
  • When does a Pl/sql function depends on session-specific context?
    1. when it directly invokes SQL function SYS_CONTEXT
    2. Indirectly invokes SYS_CONTEXT by using Virtual Private Database mechanisms for fine-grained security.
  • If you must cache the results of a function that depends on session-specific application contexts, you must pass the application context to the function as a parameter.


Using Application Context and the Result Cache
  • Assume that config_tab table has a VPD policy that translates query A into query B below::
SELECT value from config_tab where name = param_name;    -- Query A

SELECT value from config_tab                             -- Query B
where name = param_name
And app_id = sys_context('Cofig', 'App_ID');  --VPD policy adds to the WHERE clause

  • To use the RESULT CACHE, the application context needs to be passed as parameter to the function:
create or replace function get_param_value (
 param_name varchar2,
 appctx     varchar2 default sys_context('Config', 'App_ID')) -- pass app context as parameter
 RETURN VARCHAR
 RESULT_CACHE
IS
 rec varchar2(2000);
BEGIN
 select val into rec
 from config_tab
 where name = param_name;
 RETURN rec;
END;

Execution Right: Invoker's vs. Definer's
  • A program unit can execute within the permissions of the user that created the program (definer's right) or within the permissions of the user that invokes the program (invoker's right).
  • To set execution right: AUTHID = { CURRENT_USER | DEFINER (default)}
  • DBA_PROCEDURES: Check AUTHID values.

  • You can define AUTHID for: CREATE FUNCTION, CREATE PACKAGE, CREATE PROCEDURE, CREATE TYPE, and ALTER TYPE.
  • Procedures and Functions take the definer's right by default.
  • An anonymous block always behaves like an Invoker's Right (CURRENT_USER) unit.
  • A trigger or view always behaves like a Definer's Right (DEFINER) unit.
  • You can create a database link to use invoker's rights:
  • CREATE DATABASE LINK link_name CONNECT TO CURRENT_USER USING connect_string;
  • A current-user link lets you connect to a remote database as another user, with that user's privileges.

(10) Subprograms: Blocks and Stored Procedures



Advantages of subprograms:
  1. Modularity
  2. Easier Application design
  3. Maintainability
    • If underlying objects change: only subprogram needs to be modified.
  4. Better code clarity
  5. Packageability
  6. Reusability: Can be shared by many applications in the database.
  7. Better performance
    • subprograms stored in p-code (exec) form. (except for FUNCTIONS)
    • send a set of statements to the server in a group instead of sending individual statements.
    • Reduced network traffic
  8. Data Security and Integrity:
    • Ensure that all the activity on related tables is either performed in its entirety or not performed at all.
    • Inherit the privileges of the owner by default. (AUTHID DEFINER)


Stored procedures
  • Procedures inherit the privileges of the owner by default
  • You can use procedures to grant indirect access to the objects
  • When a procedure is created it is compiled and source code and compiled form (p-code) are stored in the database.
  • Procedures are stored in the database irrespective of whether or not the compilation was successfull.
  • If compilation fails, USER_OBJECTS display that procedure's status as INVALID, and the procedure will FAIL to execute.
  • If there are any modifications in the objects which the procedure references, it will become INVALID and will fail to execute.
  • When called, the p-code is read from the disk and stored in the shared pool area (SGA).
  • Check *_PROCEDURES, *_SOURCE and *_OBJECTS for data dict info on stored procedures.


---- USER A: ----
create table emp
 (name varchar2(10),  sal  number);

insert into emp 
  values ('John', 1000);
commit;

create or replace procedure ins_emp 
              (name varchar2, salary number)
as
begin
 insert into emp values(name, salary);
 dbms_output.put_line('Insert: Success');
 commit;
exception
 when others then 
   dbms_output.put_line('Error: ' || 
              dbms_utility.format_error_stack());
end;
/

set serveroutput on 
exec ins_emp('Mary', 12000);

select * from emp;
NAME           SAL
---------- -------
John          1000
Mary          1200
---- USER B: ----
select * from a.emp;

SQL Error: ORA-00942: table or view does not exist
00942. 00000 -  "table or view does not exist"


---- USER A: ----
grant execute on ins_emp to b;


---- USER B: ----
set serveroutput on
exec a.ins_emp('BJoe', 9000);

anonymous block completed
Insert: Success


---- USER A: ----
select * from emp;
NAME           SAL
---------- -------
John          1000
Mary          1200
BJoe          9000


Granting indirect access to db objects.
Scenario:


User A:
  • User A Owns table emp.
  • User A needs to control access to emp, but still allow other users to add new employees.
  • User A creates procedure ins_emp.










User B:
  • User B do not have access to directly read or write into table A.emp.
  • SELECT from B on A.emp fails


User A:
  • User A grants EXECUTE PROCEDURE on emp to User B.

User B:
  • User B executes A.ins_emp.
  • A.ins_emp runs with owner's privilege and inserts into A.emp.








Formal parameters can be only one of these:
  1. UNCONSTRAINED type name (number, varchar)
  2. Type constrained using %TYPE or %ROWTYPE

  • Using %TYPE is recommended, because if the type of the column in the table changes, it is not necessary to change the application code.
  • If the get_emp_names procedure is part of a package, you can use previously-declared public (package) variables to constrain its parameter data types. For example:
    dept_number NUMBER(2);
    ...
    PROCEDURE get_emp_names(dept_num IN dept_number%TYPE);
  • Parameters can take initial values. Use either the assignment operator or the DEFAULT keyword to give a parameter an initial value. For example, these are equivalent:
    PROCEDURE Get_emp_names (Dept_num IN NUMBER := 20) IS ...
    PROCEDURE Get_emp_names (Dept_num IN NUMBER DEFAULT) IS ...
  • Privileges required to compile the subprogram or package successfully):
    - The owner of the subprogram or package must be explicitly granted the necessary object privileges for all objects referenced within the body of the code.
    - The owner cannot obtain required privileges through roles.
  • Package creation requires a sort. The user creating the package must be able to create a sort segment in the temporary tablespace with which the user is associated.




Procedure lists employees of a department
CREATE OR REPLACE procedure list_emp(p_dept in 
                            employees.department_id%type default 80) as 
 ln    employees.last_name%type;
 sal   employees.salary%type;
 dept_out_of_range EXCEPTION;
 cursor c1 (p_dept in employees.department_id%type)  IS
   select last_name, salary 
   from employees
   where department_id = p_dept
   order by last_name;
BEGIN
  IF p_dept < 0 or p_dept > 200 THEN
    RAISE dept_out_of_range;
  END IF;
  Open c1(p_dept);
  loop 
    fetch c1 into ln, sal;
    exit when c1%NOTFOUND;
    dbms_output.put_line('Dept '|| p_dept || ', '|| ln ||', '|| sal);
  end loop;
  dbms_output.put_line('------ END List ----- ');
EXCEPTION
 when dept_out_of_range then 
   dbms_output.put_line('Department '|| p_dept || ' is not a valid department');
  dbms_output.put_line(sqlerrm);
   dbms_output.put_line(dbms_utility.format_error_backtrace());
 when others then 
   dbms_output.put_line(sqlerrm);
   dbms_output.put_line(dbms_utility.format_error_backtrace());
END list_emp;

set serveroutput on
begin
 list_emp(40);
end;

PROCEDURE list_emp compiled
anonymous block completed
Dept 40, Mavris, 6500
------ END List ----- 

-- IF you use an invalid department ID: 
set serveroutput on
begin
 list_emp(300);
end;

anonymous block completed
Department 300 is not a valid department
User-Defined Exception
ORA-06512: at DEV2.LIST_EMP", line 13


Procedure prints last name and salary of all the
employees in the same department as the user given
CREATE OR REPLACE procedure list_emp2(p_user in 
                            employees.last_name%type) as 
 ln    employees.last_name%type;
 sal   employees.salary%type;
 user_not_found EXCEPTION;
 dept employees.department_id%type;
 
 cursor c1 (p_dept in employees.department_id%type)  IS
   select last_name, salary 
   from employees
   where department_id = p_dept
   order by last_name;

 FUNCTION get_dept (p_user in varchar2)
  RETURN employees.department_id%type
 AS 
  v_dept departments.department_id%type;
 BEGIN
   select department_id into v_dept
   from employees 
   where upper(employees.last_name) = upper(p_user);
   RETURN v_dept;
 EXCEPTION
   WHEN no_data_found then 
     RETURN -20;
 END get_dept;

BEGIN
  dept := get_dept(p_user);
  IF dept = -20 then 
    RAISE user_not_found;
  ELSE
    Open c1(dept);
    loop 
      fetch c1 into ln, sal;
      exit when c1%NOTFOUND;
      dbms_output.put_line('Dept '|| dept || ', '|| ln ||', '|| sal);
    end loop;
    dbms_output.put_line('------ END List ----- '); 
  END IF;

EXCEPTION
 when user_not_found then 
   dbms_output.put_line('Invalid Last Name: '|| p_user);
   dbms_output.put_line(sqlerrm);
 when others then 
   dbms_output.put_line(sqlerrm);
   dbms_output.put_line(dbms_utility.format_error_backtrace());
END list_emp2;


set serveroutput on 
begin
 list_emp2('Mavris');
end;
/
anonymous block completed
Dept 40, Mavris, 6500
------ END List ----- 

set serveroutput on 
begin
 list_emp2('Joelma');
end;
/
anonymous block completed
Invalid Last Name: Joelma
User-Defined Exception


Forward Declaration
  • A forward declaration declares a nested subprogram but does not define it.
  • You must define it later in the same block.
  • Forward declaration and the definition must have the same subprogram heading.

DECLARE
  -- Declare proc1 (forward declaration):
  PROCEDURE proc1(number1 NUMBER);

  -- Declare and define proc2:
  PROCEDURE proc2(number2 NUMBER) IS
  BEGIN
    proc1(number2);
  END;

  -- Define proc 1:
  PROCEDURE proc1(number1 NUMBER) IS
  BEGIN
    proc2 (number1);
  END;

BEGIN
  NULL;
END;
/

About Parameters
  • Can be passed BY reference or BY value.
  • BY REFERENCE: Pointer to actual parameter. ACTUAL and FORMAL parameter refer to the SAME memory location.
  • BY VALUE: actual and formal parameters refer to different memory locations.
  • IN mode: Actual param passed by reference. But FORMAL param CANNOT be changed.
  • OUT mode: Actual param passed by value.
    • If you specify NOCOPY, it might be passed by reference.
    • Formal param: unitialized variable. Has NULL as initial value. Subprogram should ASSIGN a value. (So PL/SQL might COPY value from Formal to Actual at the end).
    • NOCOPY is only a hint: each time the subprogram is invoked, the compiler decides, silently, whether to obey or ignore NOCOPY.
  • IN OUT mode: Actual parameter is passed by value (in both directions).
  • OUT and IN OUT:
    • (a) if subprogram exited success: value of formal param COPIED to the acutal param.
    • (b) If failure: formal value not passed. Actual retain previous value.
  • IN parameters CAN be assigned a DEFAULT value when declared.
  • OUT and IN OUT parameters CANNOT be assigned DEFAULT values when declared.
  • PLS-00312: a POSITIONAL parameter association MAY NOT follow a named association

Default values and Constraints
If the datatype of a formal parameter is a constrained subtype the:
  • if the subtype has a NOT NULL constraint, the ACTUAL parameter inherits it.
  • if the subtype has the base type VARCHAR2, then ACTUAL param does not inherit the SIZE of subtype
  • if subtype is numeric, actual param inherits the range, but not scale, size or precision.

set serveroutput on
declare
subtype license is varchar2(7) not null;
n license := 'DLLLDDD';

procedure p (x License) IS 
begin 
dbms_output.put_line(x);
end;

begin
p('1ABC123456788');
p(NULL);
end; 
/
Error report:
ORA-06550: line 12, column 5:
PLS-00567: cannot pass NULL to a NOT NULL constrained formal parameter
ORA-06550: line 12, column 3:
PL/SQL: Statement ignored


Parameter Aliasing
  • Aliasing: having TWO different names for the same memory location.
  • Always occur when parameter is passed by reference.
  • Can also occur when a subprogram has cursor variable parameters.
  • Aliasing can occur for one invocation but not another, making subprogram results indeterminate. For example:
    • If actual parameter is a global variable: an assignment to the formal parameter MAY show in the global parameter
    • If the same variable is the actual parameter for two formal parameters:, then an assignment to either formal parameter might show immediately in both formal parameters.
    • If the actual parameter is a package variable: an assignment to either the formal parameter or the package variable might show immediately in both the formal parameter and the package variable.
    • If the subprogram is exited with an unhandled exception: then an assignment to the formal parameter might show in the actual parameter.


CASE 1: word_list is formal parameter, with NOCOPY option.
IF compiler obeys the NOCOPY: Any update to the IN OUT parameter will reflect
immediately in the lexicon global variable.
IF compile DOES NOT obey the NOCOPY directive: any update to the IN OUT
parameter will only show up in the global var after a successful completion
of the procedure.
set serveroutput on 
DECLARE 
 type definition is record (
   word  varchar2(20), 
   meaning varchar2(200));

 type dictionary is varray(2000) of definition;
 lexicon dictionary := dictionary();  -- global variable
 
 procedure add_entry (
   word_list IN OUT  NOCOPY dictionary  -- formal param no copy (should point to actual param)
 ) IS
 Begin
  word_list(1).word := 'aardvark';
 end;

BEGIN
  lexicon.EXTEND;
  lexicon(1).word := 'aardwolf';
  add_entry(lexicon);    -- global variable is actual parameter
  dbms_output.put_line(lexicon(1).word);
END;
/
anonymous block completed
aardvark


CASE 2: word_list is formal parameter, with NOCOPY option.
Here an exception is raised within the procedure.
if NOCOPY was obeyed the global variable will have its
value updated despite the procedure failure..
set serveroutput on 
DECLARE 
 type definition is record (
   word  varchar2(20), 
   meaning varchar2(200));
 type dictionary is varray(2000) of definition;
 lexicon dictionary := dictionary();  -- gloval variable

procedure add_entry (
   word_list IN OUT NOCOPY dictionary  -- formal param no copy
 ) IS
   e1 exception;
 Begin
  word_list(1).word := 'aardvark';
  raise e1;
 end;

BEGIN
  lexicon.EXTEND;
  lexicon(1).word := 'aardwolf';
  add_entry(lexicon);    -- global variable is actual parameter
  dbms_output.put_line(lexicon(1).word);
EXCEPTION 
 when others then 
    dbms_output.put_line(sqlerrm);
    dbms_output.put_line(lexicon(1).word);
END;
/
anonymous block completed
User-Defined Exception
aardvark


Aliasing from cursor variables
  • Cursor variable parameters are pointers.
  • Therefore, if a subprogram assigns one cursor variable parameter to another,
    they refer to the same memory location.
  • This aliasing can have unintended results.


(1) The procedure has two cursor variable parameters, emp_cv1 and emp_cv2.
(2) The procedure opens emp_cv1 and assigns its value (which is a pointer) to emp_cv2.
(3) Now emp_cv1 and emp_cv2 refer to the same memory location.
(4) When the procedure closes emp_cv1, it also closes emp_cv2.
(5) Therefore, when the procedure tries to fetch from emp_cv2, PL/SQL raises an exception.
DECLARE
  TYPE EmpCurTyp IS REF CURSOR;
  c1 EmpCurTyp;
  c2 EmpCurTyp;

  PROCEDURE get_emp_data (
    emp_cv1 IN OUT EmpCurTyp,
    emp_cv2 IN OUT EmpCurTyp
  )
  IS
    emp_rec employees%ROWTYPE;
  BEGIN
    OPEN emp_cv1 FOR SELECT * FROM employees;
    emp_cv2 := emp_cv1;  -- now both variables refer to same location
    FETCH emp_cv1 INTO emp_rec;  -- fetches first row of employees
    FETCH emp_cv1 INTO emp_rec;  -- fetches second row of employees
    FETCH emp_cv2 INTO emp_rec;  -- fetches third row of employees
    CLOSE emp_cv1;  -- closes both variables
    FETCH emp_cv2 INTO emp_rec; -- causes error when get_emp_data is invoked
  END;
BEGIN
  get_emp_data(c1, c2);
END;
/
Error report:
ORA-01001: invalid cursor
ORA-06512: at line 19
ORA-06512: at line 22


Default values for PARAMETERS
  • Omitting an actual parameter does not make the value of the corresponding formal parameter NULL.
  • To make the value of a formal parameter NULL, specify NULL as either the default value or the actual parameter.

Using SELECT FOR UPDATE and testing for DEFAULT parameter VALUES
  • SELECT FOR UPDATE enables you to base an update on the existing values in the rows,
    because it ensures that no other user can change those values before you update them.
  • By default, the SELECT FOR UPDATE statement waits until the requested row lock is acquired. To change this behavior, use the NOWAIT, WAIT, or SKIP LOCKED clauses.
  • When SELECT FOR UPDATE is associated with an explicit cursor, the cursor is called a FOR UPDATE cursor.
  • Only a FOR UPDATE cursor can appear in the CURRENT OF clause of an UPDATE or DELETE statement.

set serveroutput on
DECLARE
 PROCEDURE raise_sal_dept (
    deptid in employees.department_id%type,
    amount in employees.salary%type :=100,      -- default
    extra in  employees.salary%type default 50  -- default
  ) IS 
   stmt varchar2(200);
   vempid employees.employee_id%type;
   vsal   employees.salary%type;

  -- Only a FOR UPDATE cursor can appear in the CURRENT OF clause. 
  -- So, a CURSOR VARIABLE CANNOT be used here. You need to define a 
  -- FOR UPDATE CURSOR
  CURSOR vcur IS
     select employee_id, salary 
      from employees
      where department_id = deptid
      FOR UPDATE;
  BEGIN 
   OPEN vcur;
   dbms_output.put_line('Updating salaries of Dept: ' || deptid);
   LOOP
     FETCH vcur INTO vempid, vsal;
     EXIT when vcur%NOTFOUND;
     dbms_output.put_line('Updating Emp Id '|| vempid ||'. New salary: ' ||
                           vsal ||' + '|| amount || ' + '|| extra);
     UPDATE employees
     set salary = salary + amount + extra
     WHERE CURRENT OF vcur;
   END LOOP;
 END raise_sal_dept;
BEGIN
 -- dept 20: 2 employees
 -- dept 70: 1 employee
 -- dept 1:  does not exist
 -- dept 30: 6 employees
 raise_sal_dept(20);    -- same as raise_sal_dept(20, 100, 50);
END;
/
anonymous block completed
Updating salaries of Dept: 20
Updating Emp Id 201. New salary: 13150 + 100 + 50
Updating Emp Id 202. New salary: 6150 + 100 + 50


Local Subprograms
  • Local subprograms should be placed LAST in the declarative section. Otherwise, a compilation error will be generated.
  • Local subprograms can ONLY be referenced from within the main program.
  • So, local subprograms CANNOT be invoked from outside the parent subprogram.

Overloaded subprograms
  • PL/SQL lets you overload nested subprograms, package subprograms, and type methods.
  • You can use the same name for several different subprograms if their formal parameters differ in name, number, order, OR data type family

Restrictions to overloading
  • You cannot use two functions with the same name that differ only in their return data type.
  • You cannot use two procedures with the same name if their parameters differ only in name or mode.
  • You cannot overload two subprograms if their parameters belong to the same family of data types.

Q&As: Partitioning




(Q) Which of the following sentences about partitioning and data compression are true?

Basic data distribution methods: Range, Hash, and List
(a) Single-Level Partitioning
  • Range partitioning
  • Hash Partitioning
  • List Partitioning
(b) Composite Partitioning
  • Range-Range
  • Range-Hash
  • Range-List
  • List-Range
  • List-Hash
  • List-List
(c) Interval Partitioning
(d) System Partitioning
(e) Reference Partitioning
(f) Virtual column-based partitioning


(Q) Which of the following sentences about partitioning and data compression are true?

( ) Data in a partitioned table can be compressed on a partition-by-partition basis
( ) When you alter a partition to enable compression, compression is applied only to future data to be inserted
( ) When you enable compression on a partition, all data in the partition, including previously inserted data, is compressed
( ) Before adding a compressed partition to a partitioned table, you must either drop or make UNUSABLE all existing bitmap indexes
( ) You DO NOT have to drop or make unusable b-tree indexes in a table before adding a compressed partition