0% found this document useful (0 votes)
11 views

Task-10 On 12-12-2023

Uploaded by

vishnuteja2612
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)
11 views

Task-10 On 12-12-2023

Uploaded by

vishnuteja2612
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/ 8

DATA BASE MANAGEMENT SYSTEMS LAB

(AIML-B) 2023-24

DBMS LAB
Task-10: PL/SQL PROCEDURES, FUNCTIONS, CURSORS
Date: - 12-12-2023
PL/SQL subprograms are named PL/SQL blocks that can be invoked with a set of parameters.
PL/SQL provides two kinds of subprograms −

 Procedures − These subprograms do not return a value directly; mainly used to


perform an action.
 Functions − These subprograms return a single value; mainly used to compute and
return a value.
Procedures
Syntax for creating a Procedure
CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
BEGIN
< procedure_body >
END [procedure_name];

Example:
CREATE OR REPLACE PROCEDURE greetings
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
/

To execute the Procedure


EXECUTE greetings;
OR
EXEC greetings;

Calling a procedure in PL/SQL Block:


BEGIN
greetings;
END;
/

CVR COLLEGE OF ENGINEERING Mr. Hari Shankar Punna


DATA BASE MANAGEMENT SYSTEMS LAB
(AIML-B) 2023-24

Deleting a Standalone Procedure:


Syntax:
DROP PROCEDURE procedure-name;
Example:
DROP PROCEDURE greetings;

PL/SQL Program to demonstrate the Procedures by using IN Operator.


Example 1:
create or replace procedure proc1(s in varchar)
is
begin
dbms_output.put_line('Hello '||s);
end;
/
SQL>exec proc1('Hari');

Example 2:
CREATE OR REPLACE PROCEDURE addition(a IN number, b IN number) IS
c number;
BEGIN
c := a+b;
dbms_output.put_line('Sum of two nos= '|| c);
END addition;
/
--Calling a Procedure with arguments from a PL/SQL block
DECLARE
x number;
y number;
BEGIN
x := &x;
y := &y;
addition(x,y);
END;
/

CVR COLLEGE OF ENGINEERING Mr. Hari Shankar Punna


DATA BASE MANAGEMENT SYSTEMS LAB
(AIML-B) 2023-24

PL-SQL Program to demonstrate the Procedures by using OUT Operator

create or replace procedure proc2(x out varchar2) is


begin
x:='Hari Shankar';
dbms_output.put_line('Welcome '||x);
end;
/
--Receiving a parameter from Procedure to PL/SQL Block
declare
temp varchar2(20);
begin
proc2(temp);
dbms_output.put_line('output is '||temp);
end;
/
PL-SQL Program to demonstrate the Procedures by using IN, OUT Operator

DECLARE
a number;
b number;
c number;
PROCEDURE findMin(x IN number, y IN number, z OUT number) IS
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
BEGIN
a:= 23;
b:= 45;

CVR COLLEGE OF ENGINEERING Mr. Hari Shankar Punna


DATA BASE MANAGEMENT SYSTEMS LAB
(AIML-B) 2023-24

findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;
/
Functions
Syntax for creating a Function:
CREATE [OR REPLACE] FUNCTION function_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
-- PL/SQL subprogram body;
EXCEPTION
-- Exception Handling block ;
END [function_name];

Example 1:
create or replace function tot1
return number is
total number:=0;
begin
select count(*) into total from sailors;
return total;
end;
/
--Calling Function in PL/SQL Block
declare
c number(2);
begin
c:=tot1();
dbms_output.put_line('total number of sailors: '||c);
end;
/

CVR COLLEGE OF ENGINEERING Mr. Hari Shankar Punna


DATA BASE MANAGEMENT SYSTEMS LAB
(AIML-B) 2023-24

Example 2:
-- Create a function with two input parameters
CREATE OR REPLACE FUNCTION concat_strings (str1 IN VARCHAR2,str2 IN
VARCHAR2)
RETURN VARCHAR2
IS
-- Declare a variable to store the concatenated result
result VARCHAR2(100);

BEGIN
-- Concatenate the two input strings
result := str1 || ' ' || str2;

-- Return the concatenated result


RETURN result;
END concat_strings;
/

DECLARE
result_str VARCHAR2(100);
BEGIN
-- Call the function with two strings
result_str := concat_strings('Hari', 'Shankar');
DBMS_OUTPUT.PUT_LINE('The concatenated string is: ' || result_str);
END;
/

Example 3:
The following program calculates the factorial of a given number by calling itself recursively

DECLARE
num number;
factorial number;

CVR COLLEGE OF ENGINEERING Mr. Hari Shankar Punna


DATA BASE MANAGEMENT SYSTEMS LAB
(AIML-B) 2023-24

CREATE OR REPLACE FUNCTION fact(x number) RETURN number


IS
f number; BEGIN
IF x=0 THEN f := 1;
ELSE
f := x * fact(x-1); END IF;
RETURN f;
END;

BEGIN
num:= &num;
factorial := fact(num);
dbms_output.put_line(' Factorial '|| num || ' is ' || factorial); END;
/
PL/SQL - Cursors

A cursor is a pointer to this context area. PL/SQL controls the context area through a cursor.
A cursor holds the rows (one or more) returned by a SQL statement. The set of rows the
cursor holds is referred to as the active set.

There are two types of cursors −


1. Implicit Cursors
Implicit cursors are automatically created by Oracle whenever an SQL statement is executed,
when there is no explicit cursor for the statement.
In PL/SQL, you can refer to the most recent implicit cursor as the SQL cursor, which always
has attributes such as %FOUND, %NOTFOUND, %ISOPEN and %ROWCOUNT.

DECLARE
total_rows number(2);
BEGIN
UPDATE employees
SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no employees selected');

CVR COLLEGE OF ENGINEERING Mr. Hari Shankar Punna


DATA BASE MANAGEMENT SYSTEMS LAB
(AIML-B) 2023-24

ELSIF sql%found THEN


total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' employees selected ');
END IF;
END;
/
2. Explicit Cursors
Explicit cursors are programmer-defined cursors for gaining more control over the context
area. An explicit cursor should be defined in the declaration section of the PL/SQL Block. It
is created on a SELECT Statement which returns more than one row.
Working with MySQL cursor:

Step:1 Declare a cursor as follows:

CURSOR cursor_name IS select_statement;

The cursor declaration must be after any variable declaration. If you declare a cursor before variables
declaration, MySQL will issue an error. A cursor must always be associated with a SELECT statement.

Step:2 : Open the cursor by using the OPEN statement. The OPEN statement initializes the result set
for the cursor, therefore, you must call the OPEN statement before fetching rows from the result set.

OPEN cursor_name;

Step:3: FETCH statement to retrieve the next row pointed by the cursor and move the cursor to the
next row in the result set.

FETCH cursor_name INTO variables list;

Step:4: CLOSE statement to deactivate the cursor and release the memory associated with it as
follows:

CLOSE cursor_name;

Example

DECLARE
e_id employees.emp_id%type;
e_name employees.emp_name%type;

CVR COLLEGE OF ENGINEERING Mr. Hari Shankar Punna


DATA BASE MANAGEMENT SYSTEMS LAB
(AIML-B) 2023-24

e_job employees.job_name%type;
CURSOR e_employees is
SELECT emp_id, emp_name, job_name FROM employees;
BEGIN
OPEN e_employees;
LOOP
FETCH e_employees into e_id, e_name, e_job;
EXIT WHEN e_employees%notfound;
dbms_output.put_line(e_id || ' ' || e_name || ' ' || e_job);
END LOOP;
CLOSE e_employees;
END;
/

CVR COLLEGE OF ENGINEERING Mr. Hari Shankar Punna

You might also like