Task-10 On 12-12-2023
Task-10 On 12-12-2023
(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 −
Example:
CREATE OR REPLACE PROCEDURE greetings
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
/
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;
/
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;
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;
/
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;
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;
BEGIN
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.
DECLARE
total_rows number(2);
BEGIN
UPDATE employees
SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no employees selected');
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.
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;
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;
/