Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

PL/SQL Procedures and Functions: Syntax, Execution, and Examples, Exams of Database Management Systems (DBMS)

The concept of Procedures and Functions in PL/SQL, their syntax, execution methods, and provides examples of creating procedures and functions. Procedures and functions are stored programs in Oracle Database that can take zero or more parameters and return no or a value respectively. They can be executed from SQL prompt or within another procedure. the creation of procedures and functions, their execution methods, and examples of creating procedures and functions.

Typology: Exams

2019/2020

Uploaded on 10/21/2020

sandhiya-rpdksm
sandhiya-rpdksm 🇮🇳

1 document

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PROCEDURES AND FUNCTIONS
AIM
To implement Procedures and Functions using queries.
DESCRIPTION
Procedures and functions are named PL/SQL programs that are stored in a compiled form in the
database. Functions take zero or more parameters and return a value. Procedures take zero or more
parameters and return no values. Both functions and procedures can receive or return zero or more values
through their parameter lists.
Pass parameters to procedures in three ways.
1) IN-parameters
2) OUT-parameters
3) IN OUT-parameters
A procedure may or may not return any value.
A function is a named PL/SQL Block which is similar to a procedure. The major difference
between a procedure and a function is, a function must always return a value, but a procedure may or may
not return a value.
PROCEDURE
General Syntax to create a procedure is
CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters]
IS
Declaration section
BEGIN
Execution section
EXCEPTION
Exception section
END;
The below example creates a procedure ‘employer_details’ which gives the details of the
employee.
CREATE OR REPLACE PROCEDURE employer_details
IS
CURSOR emp_cur IS
SELECT first_name, last_name, salary FROM emp_tbl;
emp_rec emp_cur%rowtype;
BEGIN
FOR emp_rec in sales_cur
LOOP
dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name
|| ' ' ||emp_cur.salary);
END LOOP;
END;
/
pf3
pf4
pf5

Partial preview of the text

Download PL/SQL Procedures and Functions: Syntax, Execution, and Examples and more Exams Database Management Systems (DBMS) in PDF only on Docsity!

PROCEDURES AND FUNCTIONS

AIM

To implement Procedures and Functions using queries.

DESCRIPTION

Procedures and functions are named PL/SQL programs that are stored in a compiled form in the

database. Functions take zero or more parameters and return a value. Procedures take zero or more

parameters and return no values. Both functions and procedures can receive or return zero or more values

through their parameter lists.

Pass parameters to procedures in three ways.

1) IN-parameters

2) OUT-parameters

3) IN OUT-parameters

A procedure may or may not return any value.

A function is a named PL/SQL Block which is similar to a procedure. The major difference

between a procedure and a function is, a function must always return a value, but a procedure may or may

not return a value.

PROCEDURE

General Syntax to create a procedure is

CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters]

IS

Declaration section

BEGIN

Execution section

EXCEPTION

Exception section

END;

The below example creates a procedure ‘employer_details’ which gives the details of the

employee.

CREATE OR REPLACE PROCEDURE employer_details

IS

CURSOR emp_cur IS

SELECT first_name, last_name, salary FROM emp_tbl;

emp_rec emp_cur%rowtype;

BEGIN

FOR emp_rec in sales_cur

LOOP

dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name

|| ' ' ||emp_cur.salary);

END LOOP;

END;

Procedure execution

There are two ways to execute a procedure.

1) From the SQL prompt.

EXECUTE [or EXEC] procedure_name;

2) Within another procedure – simply use the procedure name.

procedure_name;

FUNCTION

The General Syntax to create a function is:

CREATE [OR REPLACE] FUNCTION function_name [parameters]

RETURN return_datatype;

IS Declaration_section

BEGIN

Execution_section

Return return_variable;

EXCEPTION

exception section

Return return_variable;

END;

1) Return Type: The header section defines the return type of the function. The return datatype

can be any of the oracle datatype like varchar, number etc.

2) The execution and exception section both should return a value which is of the datatype

defined in the header section. For example, let’s create a frunction called

''employer_details_func' similar to the one created in stored proc

CREATE OR REPLACE FUNCTION employer_details_func

RETURN VARCHAR(20);

IS

emp_name VARCHAR(20);

BEGIN

SELECT first_name INTO emp_name

FROM emp_tbl WHERE empID = '100';

RETURN emp_name;

END;

In the above example we are retrieving the ‘first_name’ of employee with empID 100 to

variable ‘emp_name’. The return type of the function is VARCHAR which is declared in line no

2. The function returns the 'emp_name' which is of type VARCHAR as the return value in line

no 9.

Function execution

A function can be executed in the following ways.

1) Assign it to a variable.

employee_name := employer_details_func;

If ‘employee_name’ is of datatype varchar we can store the name of the employee by assigning

the return type of the function to it.

2) As a part of a SELECT statement

SELECT employer_details_func FROM dual;

3) In a PL/SQL Statements like,

dbms_output.put_line(employer_details_func);

This line displays the value returned by the function.

OUTPUT:

Enter value for salesno:s

Old 5: sano:=’&salesno’;

New 5: sano:=’s11’;

The sales amount of taahae sales order 11 is Rs

PL/SQL procedure successfully completed.

FUNCTION

SQL> create or replace function famount(salno in varchar)

-- function declaration

-- function for calculating total price

return number as amt number(10,2); -- variable declaration

begin

select price *quantity into amt from sale where salesno=salno;

-- function return statement for return total amount

return amt;

end;

Function created.

SQL> set serveroutput on

SQL> declare

--variable declaration

salno varchar(10);

begin

salno:=’&salno’;

-- function calling with output statment

dbms_output.put_line(‘The ssales amount of the sales order’\salno\’in Rs.’\famount(salno));

end;

OUTPUT:

Enter value for salno:s

Old 4: salno:=’&salno’;

New 4: salno:=’s11’;

The sales amount of the sales orders 11 in Rs.

PL/SQL procedure successfully completed.

EXPERIMENTS:

1. Create a table for personal information with name, phone number and district.

i) Write a procedure to search for information by using name and calculate the difference between

first four digit and last four digit in the phone number.

ii) Write a function to search for district and display the names which are having duplicate value

for district.

2. Create a table bank with cno, cname, balance amount and loan amount.

i) Write a procedure to find out the name of a person who have highest loan amount.

ii) Write a function to calculate and return the difference between the loan amount and balance

amount for the customer who are having loan.

3. Create a table stock with item number, item name, quantity, price and last purchase date.

i) Write a procedure to search for an item using item number and delete it if the date of last

purchase is before 1 month from the current date. Otherwise, update the current stock.

ii) Write a function to calculate and return total price of items which are having the quantity

greater than 100.

III year CSE ‘B’ sec

    1. Roll No. of student Allotted Experiments
    1. Roll No. of student Allotted Experiments