0% found this document useful (0 votes)
4 views4 pages

Functions and Procedures notes

The document explains the creation and usage of functions and procedures in Oracle Database using PL/SQL. Functions return a single value and can be used in SQL, while procedures perform actions without returning a value. Examples include functions for adding numbers, calculating annual salary, and determining years of service, as well as a procedure for updating employee salary.

Uploaded by

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

Functions and Procedures notes

The document explains the creation and usage of functions and procedures in Oracle Database using PL/SQL. Functions return a single value and can be used in SQL, while procedures perform actions without returning a value. Examples include functions for adding numbers, calculating annual salary, and determining years of service, as well as a procedure for updating employee salary.

Uploaded by

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

In Oracle Database, functions are named PL/SQL blocks that perform a specific task and return a

single value.

CREATE [OR REPLACE] FUNCTION function_name

(param1 [IN] datatype, param2 [IN] datatype, ...)

RETURN return_datatype

IS

-- variable declarations

BEGIN

-- executable statements

RETURN value;

END;

A Simple Function to Add Two Numbers

CREATE OR REPLACE FUNCTION add_numbers(a IN NUMBER, b IN NUMBER)

RETURN NUMBER

IS

result NUMBER;

BEGIN

result := a + b;

RETURN result;

END;

SELECT add_numbers(10, 20) FROM dual;

Feature Function Procedure

Return value Must return a value Does not return a value

Usage in SQL Can be used in SQL Cannot be used in SQL

Purpose Used for computations Used for actions

Function to Get Annual Salary


CREATE OR REPLACE FUNCTION get_annual_salary(p_emp_id IN NUMBER)

RETURN NUMBER

IS

v_salary NUMBER;

BEGIN

SELECT salary * 12

INTO v_salary

FROM employees

WHERE emp_id = p_emp_id;

RETURN v_salary;

EXCEPTION

WHEN NO_DATA_FOUND THEN

RETURN 0;

END;

SELECT get_annual_salary(105) FROM dual;

Function to Get Years of Service

CREATE OR REPLACE FUNCTION get_years_of_service(p_emp_id IN NUMBER)

RETURN NUMBER

IS

v_years NUMBER;

BEGIN

SELECT FLOOR(MONTHS_BETWEEN(SYSDATE, hire_date) / 12)

INTO v_years

FROM employees

WHERE emp_id = p_emp_id;


RETURN v_years;

EXCEPTION

WHEN NO_DATA_FOUND THEN

RETURN NULL;

END;

SELECT get_years_of_service(102) FROM dual;

In Oracle, a procedure is a named PL/SQL block that performs a specific task, but does not return a
value directly (unlike functions). Procedures are ideal when you want to perform actions like insert,
update, delete, or complex business logic that doesn't return a single result.

CREATE [OR REPLACE] PROCEDURE procedure_name

(param1 IN datatype, param2 OUT datatype, ...)

IS

-- Variable declarations

BEGIN

-- Executable statements

END;

Procedure to Update Employee Salary

CREATE OR REPLACE PROCEDURE update_salary(

p_emp_id IN NUMBER,

p_new_salary IN NUMBER)

IS

BEGIN

UPDATE employees

SET salary = p_new_salary

WHERE emp_id = p_emp_id;


IF SQL%ROWCOUNT = 0 THEN

DBMS_OUTPUT.PUT_LINE('No employee found with given ID.');

ELSE

DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');

END IF;

END;

BEGIN

update_salary(101, 50000);

END;

You might also like