Professional Documents
Culture Documents
Packages 2
Packages 2
PL/SQL
10-2
Managing Package Concepts
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 3
Managing Package Concepts
Purpose
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 4
Managing Package Concepts
Components of a PL/SQL Package
Package Package
specification body
Public Private
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 5
Managing Package Concepts
Components of a PL/SQL Package
Package Package
specification body
Public Private
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 6
Managing Package Concepts
Visibility of Package Components
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 7
Managing Package Concepts
Visibility of Package Components
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 8
Managing Package Concepts
Global/Local Compared to Public/Private
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 9
Managing Package Concepts
Global/Local Compared to Public/Private
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 10
Managing Package Concepts
Visibility of Global (Public) Components
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 11
Managing Package Concepts
Visibility of Global (Public) Components
Package
specification
global_var
Procedure A;
External
code
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 12
Managing Package Concepts
Managing Package Concepts
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 13
Managing Package Concepts
Visibility of Local (Private) Components
variable_1
Procedure B IS
BEGIN … END;
variable_2
Procedure A IS
Package
BEGIN
body …
END;
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 14
Managing Package Concepts
Visibility of Local (Private) Components
variable_1
Procedure B IS External
BEGIN … END; code
variable_2
Procedure A IS
Package BEGIN
body …
END;
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 15
Managing Package Concepts
Example of Package Specification:
salary_pkg:
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 16
Managing Package Concepts
Example of Package Body: salary_pkg:
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 17
Managing Package Concepts
Example of Package Body: salary_pkg:
...
PROCEDURE update_sal -- public procedure
(p_employee_id employees.employee_id%TYPE,
p_new_salary employees.salary%TYPE)
IS v_old_salary employees.salary%TYPE; -- local variable
BEGIN
SELECT salary INTO v_old_salary FROM employees
WHERE employee_id = p_employee_id;
IF validate_raise(v_old_salary, p_new_salary) THEN
UPDATE employees SET salary = p_new_salary
WHERE employee_id = p_employee_id;
ELSE
RAISE_APPLICATION_ERROR(-20210, 'Raise too high');
END IF;
END update_sal;
END salary_pkg;
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 18
Managing Package Concepts
Invoking Package Subprograms
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 19
Managing Package Concepts
Invoking Package Subprograms
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 20
Managing Package Concepts
Removing Packages
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 21
Managing Package Concepts
Viewing Packages in the Data Dictionary
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 22
Managing Package Concepts
Using USER_ERRORS
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 23
Managing Package Concepts
Using USER_ERRORS
• To see all the errors (not just the first one), you query the
USER_ERRORS dictionary table:
CREATE OR REPLACE PROCEDURE bad_proc
IS BEGIN
error_1; -- this is an error
error_2; -- this is another error
END;
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 24
Managing Package Concepts
Using USER_ERRORS
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 25
Managing Package Concepts
Adding USER_SOURCE
Join USER_SOURCE and USER_ERRORS to see a more
complete picture of the compile errors.
SELECT e.line, e.position, s.text AS SOURCE, e.text AS ERROR
FROM USER_ERRORS e, USER_SOURCE s
WHERE e.name = s.name AND e.type = s.type
AND e.line = s.line
AND e.name = 'BAD_PROC' and e.type = 'PROCEDURE'
ORDER BY e.sequence;
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 26
Managing Package Concepts
Guidelines for Writing Packages
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 27
Managing Package Concepts
Advantages of Using Packages
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 29
Managing Package Concepts
Terminology
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 30
Managing Package Concepts
Summary
PLSQL S10L2 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 31
Managing Package Concepts