4procedures Functions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

PL/SQL Procedure

PL/SQL procedure syntax:


CREATE OR REPLACE PROCEDURE <procedure_name> (
<parameterl IN/OUT <datatype>
.. )
[ IS | AS ]
<declaration_part>
BEGIN
<execution part>
EXCEPTION
<exception handling part>
END;
PL/SQL procedure header

A procedure begins with a header that specifies its name and an optional parameter list. Each
parameter can be in either IN, OUT, or INOUT mode.

PL/SQL procedure body

1) Declarative part 2) Executable part 3) Exception-handling part

Example 1: Create procedure to Calculate the Area of Rectangle:


create or replace procedure get_area
(Len in number, Wid in number, Area out number)
as
begin
Area := Len * Wid;
end;

Testing Procedure:
declare
area1 number;
begin
get_area (10, 50, area1);
dbms_output.put_line(area1);
end;
---------------
500

Example 2:
The following procedure accepts a employee id and prints out the employee’s contact information
including first name, last name, and email:

1 CREATE OR REPLACE PROCEDURE print_emp( in_emp_id NUMBER )


2 IS
3 r_emp employees%ROWTYPE;
4 BEGIN
5 -- get employee based on employee id
6 SELECT *
7 INTO r_emp
8 FROM employees
9 WHERE employee_id = in_emp_id;
10 -- print out employee's information
11 dbms_output.put_line( r_emp.first_name || ' ' ||
12
13
r_emp.last_name || '<' || r_emp.email ||'>' );
14
EXCEPTION
15
WHEN OTHERS THEN
16
dbms_output.put_line( SQLERRM );
17
END;
18
/
19
20

Example 3:
We’re going to develop a procedure named adjust_salary() in HR sample database provided by
Oracle. We’ll update the salary information of employees in the employees table by using SQL
UPDATE statement.
The following is the source code of the adjust_salary() procedure :
1

2 CREATE OR REPLACE PROCEDURE adjust_salary( in_employee_id IN EMPLOYEES.EMPLOYEE_ID%TYPE,

3 in_percent IN NUMBER ) IS

4 BEGIN

5 -- update employee's salary

6 UPDATE employees

7 SET salary = salary + salary * in_percent / 100

8 WHERE employee_id = in_employee_id;

9 END;

10

PL/SQL Function
Creating a PL/SQL function
CREATE OR REPLACE FUNCTION <procedure_name> (
<parameterl IN/OUT <datatype> ...)
RETURN <datatype>
[ IS | AS ]
<declaration_part>
BEGIN
<execution part>
EXCEPTION
<exception handling part>
END;
A function consists of a header and body.

The function header has the function name and a RETURN clause that specifies the datatype of the
returned value. Each parameter of the function can be either in the IN, OUT, or INOUT mode. The
declarative section is between the IS and BEGIN keywords. It is where you
declare variables, constants, cursors, and user-defined types. you must have at least
one RETURN statement in the executable statement.

Example 1: Print welcome message

CREATE OR REPLACE FUNCTION welcome_msgJune ( p_name IN VARCHAR2) RETURN VARCHAR2


IS
BEGIN
RETURN (‘Welcome ‘|| p_name);
END;
/
Testing Function:
DECLARE
lv_msg VARCHAR2(250);
BEGIN
lv_msg := welcome_msg_func (‘UOK9’);
dbms_output.put_line(lv_msg);
END;
SELECT welcome_msg_func(‘UOK9:) FROM DUAL;

Example 2: Count employees by City name.

CREATE OR REPLACE FUNCTION get_count_emp(V_city varchar2) RETURN NUMBER


IS
l_coun_emp NUMBER := 0;
BEGIN
-- get count of employees
SELECT count(employee_id)
INTO l_coun_emp
FROM employees
WHERE department_id in (select department_id from departments where location_id IN( select
location_id from locations where city = v_city));
-- return the count of employees
RETURN l_coun_emp;
END;

Example 3: Create a function that takes department ID and returns the name of the manager of
the department.
create or replace function GEt_Manager (d_id number) return varchar2
as
m_name varchar2(125);
m_id number;
begin
select manager_id into m_id from departments where department_id=d_id;
select first_name || ' '|| last_name
into m_name
from employees
where employee_id=m_id;
return m_name;
end;
/

Example 4: Create a function that takes employee ID and return the number of jobs done by the
employee in the past.
create or replace function Get_num (e_id number) return number
as
cn number;
begin
select count(*) into cn from job_history where employee_id=e_id;
return cn;
end;
/

Built-in Functions in PL/SQL:

Conversion Functions:

Function Name Usage


TO_CHAR Converts the other datatype to character datatype
TO_DATE ( string, format ) Converts the given string to date. The string should match with the format.
TO_NUMBER (text, Converts the text to number type of the given format. Informat '9' denotes the number of
format) digits

String Functions:

Function Name Usage


INSTR(text, string, start, occurance) Gives the position of particular text in the given string.
SUBSTR ( text, start, length) Gives the substring value of the main string.
UPPER ( text )
Returns the uppercase of the provided text

LOWER ( text )
Returns the lowercase of the provided text

INITCAP ( text)
Returns the given text with the starting letter in upper case.

LENGTH ( text )
Returns the length of the given string

LPAD ( text, length, pad_char) Pads the string in the left side for the given length (total string) with the given character
RPAD (text, length, pad_char) Pads the string in the right side for the given length (total string) with the given character
LTRIM ( text )
Trims the leading white space from the text

RTRIM ( text )
Trims the trailing white space from the text

Date Functions:

Function Name Usage


ADD_MONTHS (date, no.of months) Adds the given months to the date
SYSDATE
Returns the current date and time of the server

TRUNC
Round of the date variable to the lower possible value

ROUND
Rounds the date to the nearest limit either higher or lower

MONTHS_BETWEEN
Returns the number of months between two dates

You might also like