0% found this document useful (0 votes)
3 views16 pages

3 Oracle PLSQL Part2 m3 Slides

The document provides an overview of functions in PL/SQL, including their definition, types, and how to create, compile, and execute them. It covers Oracle-provided functions, privileges required for function creation, and examples of defining and handling exceptions in functions. Additionally, it discusses function termination and the importance of handling errors and warnings during execution.

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)
3 views16 pages

3 Oracle PLSQL Part2 m3 Slides

The document provides an overview of functions in PL/SQL, including their definition, types, and how to create, compile, and execute them. It covers Oracle-provided functions, privileges required for function creation, and examples of defining and handling exceptions in functions. Additionally, it discusses function termination and the importance of handling errors and warnings during execution.

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/ 16

Functions

!!

Pankaj Jain
!
@twit_pankajj
What is a Function?

▪ Stored Subprogram

▪ Returns Information

▪ Used in Expressions
!

Do Not Place Anything in


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

▪ Numeric Functions DECLARE


l_num NUMBER;
SELECT ABS(-123) FROM DUAL; BEGIN
□ ROUND l_num := ABS(-123);
DBMS_OUTPUT.PUT_LINE(l_num);
□ CEIL END;
123
DECLARE
▪ Character Functions l_char VARCHAR2(4);
BEGIN
□ LPAD SELECT UPPER('Test') FROM DUAL; l_char := UPPER('Test');
DBMS_OUTPUT.PUT_LINE(l_char);
END;
□ LTRIM
TEST

▪ DateTime Functions
DECLARE
□ SYSDATE l_date DATE;
BEGIN
l_date := ADD_MONTHS( TO_DATE('10-FEB-2014','DD-MON-RRRR') ,1) ;
□ SYSTIMESTAMP DBMS_OUTPUT.PUT_LINE(l_date); Do Not Place Anything in
END; This Space
! (Add watermark during editing)
Note: Warning will not appear
10-MAR-2014 during Slide Show view.
Privileges

▪ CREATE PROCEDURE

▪ CREATE ANY PROCEDURE

▪ ALTER ANY PROCEDURE

▪ EXECUTE

!
GRANT CREATE PROCEDURE TO demo;
GRANT CREATE ANY PROCEDURE TO demo;
GRANT ALTER ANY PROCEDURE TO demo;
!
GRANT EXECUTE ON <schema>.<procedure_name> TO demo;

Do Not Place Anything in


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

CREATE [OR REPLACE] FUNCTION [schema.]<function_name>

RETURN <datatype> IS | AS

<declaration section>

BEGIN

statements;

RETURN <datatype>;

[EXCEPTION]

END [<function_name>];
Do Not Place Anything in
This Space
(Add watermark during editing)
Note: Warning will not appear
during Slide Show view.
Simple Function

!
CREATE OR REPLACE FUNCTION get_emp_count RETURN NUMBER AS
CURSOR cur_get_dept_id IS
SELECT dept_id
FROM departments
WHERE dept_name = 'IT';
l_dept_id departments.dept_id%TYPE;
l_count NUMBER := 0;
BEGIN
OPEN cur_get_dept_id;
FETCH cur_get_dept_id INTO l_dept_id;
CLOSE cur_get_dept_id;
SELECT count(*)
INTO l_count
FROM employee
WHERE emp_dept_id = l_dept_id;
RETURN l_count;
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 get_emp_count; This Space
(Add watermark during editing)
Note: Warning will not appear
during Slide Show view.
Compiling Function

! get_emp_count.sql
CREATE OR REPLACE FUNCTION get_emp_count RETURN NUMBER AS
CURSOR cur_get_dept_id IS
SELECT dept_id
FROM departments
WHERE dept_name = 'IT';
l_dept_id departments.dept_id%TYPE;
l_count NUMBER := 0;
BEGIN
OPEN cur_get_dept_id;
FETCH cur_get_dept_id INTO l_dept_id; @C:\Demo\ get_emp_count.sql
CLOSE cur_get_dept_id;
SELECT count(*)
INTO l_count
FROM employee
WHERE emp_dept_id = l_dept_id;
RETURN l_count;
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 get_emp_count; This Space
/ (Add watermark during editing)
Note: Warning will not appear
ALTER FUNCTION get_emp_count COMPILE; during Slide Show view.
Compiling Function

▪ PLSQL_CODE_TYPE !
ALTER SESSION SET PLSQL_CODE_TYPE=NATIVE;
□ NATIVE !
ALTER FUNCTION get_emp_count COMPILE PLSQL_CODE_TYPE=NATIVE;
□ INTERPRETED

!
▪ PLSQL_OPTIMIZE_LEVEL
□ 0-3 !
ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=2;

!
▪ Debug Mode
!
ALTER FUNCTION get_emp_count COMPILE DEBUG;
Do Not Place Anything in
This Space
(Add watermark during editing)
Note: Warning will not appear
during Slide Show view.
Syntax Errors
Errors !
Invalid Object Name

Severe Enable
! !
Warnings Performance Disable
! !
Informational Error

ALTER SESSION SET PLSQL_WARNINGS='ENABLE:ALL';


ALTER FUNCTION get_emp_count
COMPILE PLSQL_WARNINGS='ENABLE:PERFORMANCE', 'ERROR:SEVERE', 'ERROR:06002' REUSE SETTINGS;
SHOW ERRORS;
Do Not Place Anything in
This Space
call dbms_warning.add_warning_setting_cat('ALL', 'ENABLE', 'SESSION'); (Add watermark during editing)
Note: Warning will not appear
during Slide Show view.
Executing Function

▪ PL/SQL Block or Subprogram

▪ SQL Statement

DECLARE
l_return NUMBER;
!
BEGIN
!
l_return := get_emp_count;
!
END;

select get_emp_count from dual;

1
Do Not Place Anything in
This Space
(Add watermark during editing)
Note: Warning will not appear
during Slide Show view.
Executing Function

EXEC[UTE] :bind_variable := <function_name>;

VARIABLE l_return NUMBER;

EXEC :l_return := get_emp_count;


EXECUTE :l_return := get_emp_count; 1
!
PRINT :l_return

VARIABLE l_return NUMBER;


BEGIN
:l_return := get_emp_count;
END; 1
/
PRINT :l_return

Do Not Place Anything in


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

DROP FUNCTION <function_name>;

!
DROP FUNCTION get_emp_count;

Do Not Place Anything in


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

▪ Normal Completion
!
CREATE OR REPLACE FUNCTION get_emp_count RETURN NUMBER AS
CURSOR cur_get_dept_id IS
SELECT dept_id
FROM departments
WHERE dept_name = 'IT';
l_dept_id departments.dept_id%TYPE;
l_count NUMBER := 0;
BEGIN
OPEN cur_get_dept_id;
FETCH cur_get_dept_id INTO l_dept_id;
CLOSE cur_get_dept_id;
SELECT count(*)
INTO l_count
FROM employee
WHERE emp_dept_id = l_dept_id;
DBMS_OUTPUT.PUT_LINE('Finished Successfully');
RETURN l_count;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM); Do Not Place Anything in
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE); This Space
(Add watermark during editing)
RAISE; Note: Warning will not appear
END get_emp_count; during Slide Show view.
Exception Causing Function Termination
!
CREATE OR REPLACE FUNCTION get_emp_count RETURN NUMBER AS
CURSOR cur_get_dept_name IS
SELECT dept_name
FROM departments
WHERE dept_name = 'IT';
l_dept_id departments.dept_id%TYPE;
l_count NUMBER := 0;
BEGIN
OPEN cur_get_dept_name;
FETCH cur_get_dept_name INTO l_dept_id;
CLOSE cur_get_dept_name;
SELECT count(*)
INTO l_count
ORA-06503: PL/SQL:
FROM employee
Function returned without value
WHERE emp_dept_id = l_dept_id;
DBMS_OUTPUT.PUT_LINE('Finished Successfully');
RETURN l_count;
EXCEPTION
WHEN OTHERS THEN
IF cur_get_dept_name%ISOPEN THEN
CLOSE cur_get_dept_name;
END IF;
Do Not Place Anything in
DBMS_OUTPUT.PUT_LINE(SQLERRM); This Space
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE); (Add watermark during editing)
! RAISE; Note: Warning will not appear
END get_emp_count; during Slide Show view.
Function Termination

▪ Explicitly
! !
CREATE OR REPLACE FUNCTION get_tier RETURN CREATE OR REPLACE FUNCTION get_tier RETURN
NUMBER AS NUMBER AS
l_salary NUMBER := 50000; l_salary NUMBER := 50000;
BEGIN l_return NUMBER;
IF l_salary < 40000 THEN BEGIN
RETURN 1; IF l_salary < 40000 THEN
ELSIF l_salary < 60000 THEN l_return := 1;
RETURN 2; ELSIF l_salary < 60000 THEN
ELSE l_return := 2;
RETURN 3; ELSE
END IF; l_return := 3;
DBMS_OUTPUT.PUT_LINE('Finished Successfully'); END IF;
EXCEPTION DBMS_OUTPUT.PUT_LINE('Finished Successfully');
WHEN OTHERS THEN RETURN l_return;
DBMS_OUTPUT.PUT_LINE(SQLERRM); EXCEPTION
RAISE; WHEN OTHERS THEN
END get_tier; DBMS_OUTPUT.PUT_LINE(SQLERRM);
RAISE;
END get_tier;
Do Not Place Anything in
This Space
(Add watermark during editing)
Note: Warning will not appear
during Slide Show view.
Need for Functions
!
Summary Errors & Warnings
!
Function Operations
!

Do Not Place Anything in


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

You might also like