0% found this document useful (0 votes)
2 views9 pages

5 Oracle PLSQL Part2 m5 Slides

The document discusses local subprograms in PL/SQL, detailing their scope, visibility, and the use of local procedures and functions within stored procedures and anonymous blocks. It includes examples demonstrating how to declare and use local procedures and functions, as well as how to handle exceptions. The document emphasizes the importance of understanding variable scope and visibility in the context of local subprograms.

Uploaded by

codingera.ai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

5 Oracle PLSQL Part2 m5 Slides

The document discusses local subprograms in PL/SQL, detailing their scope, visibility, and the use of local procedures and functions within stored procedures and anonymous blocks. It includes examples demonstrating how to declare and use local procedures and functions, as well as how to handle exceptions. The document emphasizes the importance of understanding variable scope and visibility in the context of local subprograms.

Uploaded by

codingera.ai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Local Subprograms

!!

Pankaj Jain
!
@twit_pankajj
Local Subprograms

Scope From Point of


Declaration Section Declaration to End of End of Declaration
Block

Parent Block Variables


Eliminate Repetition Multiple
Visible

Do Not Place Anything in


This Space
(Add watermark during editing)
Note: Warning will not appear
during Slide Show view.
Local Procedure in a Stored Procedure

!
CREATE OR REPLACE PROCEDURE update_dept(p_emp_id employee.emp_id%TYPE)
AS
l_dept_id departments.dept_id%TYPE := 2;
!
PROCEDURE display_message(p_location IN VARCHAR2, p_msg VARCHAR2) IS
BEGIN
DBMS_OUTPUT.PUT_LINE('***'||p_location||'***');
DBMS_OUTPUT.PUT_LINE(p_msg);
END display_message;
!
BEGIN ***Before Updating***
display_message('Before Updating', 'Input Employee ID:'||p_emp_id); Input Employee ID:10
UPDATE employee
SET emp_dept_id = l_dept_id
WHERE emp_id = p_emp_id;
***After Updating***
display_message('After Updating', 'Rows Updated:' ||SQL%ROWCOUNT);
Rows Updated:1
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
RAISE; Do Not Place Anything in
END update_dept; This Space
(Add watermark during editing)
Note: Warning will not appear
during Slide Show view.
Local Procedure in Anonymous Block

!
DECLARE
l_emp_id := 10;
l_dept_id departments.dept_id%TYPE := 2;
!
PROCEDURE display_message(p_location IN VARCHAR2, p_msg VARCHAR2) IS
BEGIN
DBMS_OUTPUT.PUT_LINE('***'||p_location||'***');
DBMS_OUTPUT.PUT_LINE(p_msg);
END display_message;
!
BEGIN ***Before Updating***
display_message('Before Updating', 'Input Employee ID:'|| l_emp_id); Input Employee ID:10
UPDATE employee
SET emp_dept_id = l_dept_id
WHERE emp_id = p_emp_id;
***After Updating***
display_message('After Updating', 'Rows Updated:' ||SQL%ROWCOUNT);
Rows Updated:1
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
RAISE; Do Not Place Anything in
END ; This Space
(Add watermark during editing)
Note: Warning will not appear
during Slide Show view.
Local Function in a Stored Procedure
CREATE OR REPLACE PROCEDURE determine_tiers AS
l_salary NUMBER := 50000;
l_tier NUMBER;
FUNCTION get_tier RETURN NUMBER IS
l_return NUMBER;
BEGIN
IF l_salary < 40000 THEN
l_return := 1;
ELSIF l_salary < 60000 THEN
l_return := 2;
ELSE
l_return := 3;
END IF;
RETURN l_return;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM|| ' ' || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
RAISE;
END get_tier;
BEGIN
l_tier := get_tier; 2
l_salary := 120000;
l_tier := get_tier; 3
EXCEPTION Do Not Place Anything in
WHEN OTHERS THEN This Space
DBMS_OUTPUT.PUT_LINE(SQLERRM|| ' '||DBMS_UTILITY.FORMAT_ERROR_BACKTRACE); (Add watermark during editing)
RAISE; Note: Warning will not appear
during Slide Show view.
END determine_tiers;
Exception in a Local Subprogram
CREATE OR REPLACE PROCEDURE determine_tiers AS
l_salary NUMBER := 50000;
l_tier NUMBER;
FUNCTION get_tier RETURN NUMBER IS
l_return NUMBER;
BEGIN
IF l_salary < 40000 THEN
l_return := 1;
ELSIF l_salary < 60000 THEN
l_return := 'B';
ELSE
l_return := 3;
END IF;
RETURN l_return;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM|| ' ' || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
RAISE;
END get_tier;
BEGIN
l_tier := get_tier;
l_salary := 120000;
l_tier := get_tier;
EXCEPTION Do Not Place Anything in
WHEN OTHERS THEN This Space
(Add watermark during editing)
DBMS_OUTPUT.PUT_LINE(SQLERRM|| ' '||DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
Note: Warning will not appear
RAISE; during Slide Show view.
END determine_tiers;
Visibility of Variables
CREATE OR REPLACE PROCEDURE determine_tiers AS
l_salary NUMBER := 50000;
l_tier NUMBER;
FUNCTION get_tier RETURN NUMBER IS
l_salary NUMBER := 30000;
l_return NUMBER;
BEGIN
IF l_salary < 40000 THEN
l_return := 1;
ELSIF l_salary < 60000 THEN
l_return := 2;
ELSE
l_return := 3;
END IF;
RETURN l_return;
EXCEPTION
WHEN OTHERS THEN
RAISE;
END get_tier;
BEGIN
l_tier := get_tier; 1
l_salary := 120000;
l_tier := get_tier; 1
EXCEPTION Do Not Place Anything in
WHEN OTHERS THEN This Space
DBMS_OUTPUT.PUT_LINE(SQLERRM|| ' '||DBMS_UTILITY.FORMAT_ERROR_BACKTRACE); (Add watermark during editing)
Note: Warning will not appear
RAISE;
during Slide Show view.
END determine_tiers;
Scope of Variables
CREATE OR REPLACE PROCEDURE determine_tiers AS
l_salary NUMBER := 50000;
l_tier NUMBER;
PROCEDURE get_tier IS
BEGIN
IF l_salary < 40000 THEN
l_tier := 1;
ELSIF l_salary < 60000 THEN
l_tier := 2;
ELSE
l_tier := 3;
END IF;
EXCEPTION
WHEN OTHERS THEN
RAISE;
END get_tier;
BEGIN
get_tier;
DBMS_OUTPUT.PUT_LINE(l_tier); 2
l_salary := 120000;
get_tier;
DBMS_OUTPUT.PUT_LINE(l_tier); 3
EXCEPTION
Do Not Place Anything in
WHEN OTHERS THEN This Space
DBMS_OUTPUT.PUT_LINE(SQLERRM|| ' '||DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
(Add watermark during editing)
RAISE; Note: Warning will not appear
END determine_tiers; during Slide Show view.
Need for Local Subprograms
!
Summary Exceptions in Local Subprograms
!
Scope & Visibility of Variables
!

Do Not Place Anything in


This Space
(Add watermark during editing)
Note: Warning will not appear
during Slide Show view.

You might also like