RDBMS_Unit6
RDBMS_Unit6
1
10/7/2019
2
10/7/2019
Example
execute a procedure
3
10/7/2019
BEGIN
greetings;
END;
example
The below example creates a procedure ‘employer_details’ which gives the details of the
employee.
4
10/7/2019
5
10/7/2019
OUT
An OUT parameter returns a value to the calling program.
Inside the subprogram, an OUT parameter acts like a
variable.
You can change its value and reference the value after
assigning it.
The actual parameter must be variable and it is passed by
value.
6
10/7/2019
IN OUT
An IN OUT parameter passes an initial value to a
subprogram and returns an updated value to the caller.
It can be assigned a value and the value can be read.
The actual parameter corresponding to an IN OUT formal
parameter must be a variable, not a constant or an
expression.
Formal parameter must be assigned a value. Actual
parameter is passed by value.
7
10/7/2019
DECLARE
a number;
b number;
c number;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;
Prepared by Shimi Biju 15
DECLARE
a number;
PROCEDURE squareNum(x IN OUT number) IS
BEGIN
x := x * x;
END;
BEGIN
a:= 23;
squareNum(a);
dbms_output.put_line(' Square of (23): ' || a);
END;
Prepared by Shimi Biju 16
8
10/7/2019
Example1:
DECLARE
empName varchar(20);
CURSOR id_cur SELECT id FROM emp_ids;
BEGIN
FOR emp_rec in id_cur
LOOP
emp_name(emp_rec.id, empName);
dbms_output.putline('The employee ' || empName || ' has id ' ||
emp-rec.id);
END LOOP;
END;
Prepared by Shimi Biju 18
9
10/7/2019
The below PL/SQL block shows how to execute the above 'emp_salary_increase' procedure.
DECLARE
CURSOR updated_sal is
SELECT empID,salary
FROM emp_tbl;
pre_sal number;
BEGIN
FOR emp_rec IN updated_sal LOOP
pre_sal := emp_rec.salary;
emp_salary_increase(emp_rec.empID, emp_rec.salary);
dbms_output.put_line('The salary of ' || emp_rec.empID ||
' increased from '|| pre_sal || ' to '||emp_rec.salary);
END LOOP;
END;
10
10/7/2019
PL/SQL Functions
11
10/7/2019
Where,
12
10/7/2019
DECLARE
n3 number;
BEGIN
n3 := sum(11,22);
dbms_output.put_line(‘Sum is: ' || n3);
END;
RETURN total;
END;
/
Prepared by Shimi Biju 26
13
10/7/2019
Calling function
DECLARE
c number(2);
BEGIN
c := totalCustomers();
dbms_output.put_line('Total no. of Customers: ' || c);
END;
DECLARE
c number(2);
BEGIN
c := totalCustomers();
dbms_output.put_line('Total no. of Customers: ' || c);
END;
14
10/7/2019
DECLARE
a number;
b number;
c number;
FUNCTION findMax(x IN number, y IN number)
RETURN number
IS
z number;
BEGIN
IF x > y THEN
z:= x;
ELSE
Z:= y;
END IF;
RETURN z;
END;
BEGIN
a:= 23;
b:= 45;
Prepared cby:= findMax(a,
Shimi Biju b); 29
15