Professional Documents
Culture Documents
19CB408 - DBMS LAB Experiment 5 To 8
19CB408 - DBMS LAB Experiment 5 To 8
AIM:
To implement implicit and explicit cursors using PL/SQL programming
ALGORITHM:
CURSORS:
create table customers(id int primary key,Name varchar(20) not null,Age int not null,Address
varchar(50) not null,Salary float not null);
insert into customers values(1,'Sri',25,'CBE',25000);
insert into customers values(2,'Hari',28,'CBE',35000);
insert into customers values(3,'Sow',28,'CHENNAI',45000);
insert into customers values(4,'Kumaran',25,'CBE',24000);
PROGRAM:
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no customers
selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers selected ');
END;
1
END IF;
END;
2
4 customers selected
1 row(s) updated.
EXPLICIT CURSORS:
PROGRAM:
DECLARE
c_id customers.id%type;
c_name customers.name%type;
c_addr customers.address%type;
CURSOR c_customers is
SELECT id, name, address FROM customers;
BEGIN
OPEN c_customers;
LOOP
FETCH c_customers into c_id, c_name,
c_addr; EXIT WHEN c_customers
%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
END LOOP;
END;
3
CLOSE c_customers;
END;
4
OUTPUT:
1 Sri CBE
2 Hari CBE
3 Sow CHENNAI
4 Kumaran CBE
Statement processed.
RESULT:
Thus the PL/SQL program to implement implicit and explicit cursor has been successfully executed.
5
EX.NO:6 GENERATE TRIGGER IN PL/SQL BLOCK
AIM:
To implement PL/SQL triggers and do the operations in database automatically.
ALGORITHM:
create or replace trigger trgrr15 after delete or update on emply101 for each row
begin
if deleting then
insert into emply102 values(:old.eid,:old.ename,:old.age,:old.salary);
elsif updating then
insert into emply102 values(:old.eid,:old.ename,:old.age,:old.salary);
end if;
end;
7
RESULT:
Thus the SQL triggers are executed successfully.
8
EX.NO:7 WRITE PL/SQL BLOCK PROGRAMS USING EXCEPTION HANDLING
AIM:
ALGORITHM:
Program:
DECLARE
c_id customers.id%type := 8;
c_name customerS.Name%type;
c_addr customers.address%type;
BEGIN
SELECT name, address INTO c_name, c_addr
FROM customers
WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such
customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
OUTPUT:
No such customer!
Statement processed.
9
Raising Exceptions
Syntax:
DECLARE
exception_name EXCEPTION;
BEGIN
IF condition THEN
RAISE exception_name;
END IF;
EXCEPTION
WHEN exception_name THEN
statement;
END;
Program:
DECLARE
myex EXCEPTION;
n NUMBER :=10;
BEGIN
FOR i IN 1..n LOOP
dbms_output.put_line(i*i);
IF i*i=36 THEN
RAISE myex;
END IF;
END LOOP;
EXCEPTION
WHEN myex THEN
RAISE_APPLICATION_ERROR(-20015, ‘ERROR’);
END;
OUTPUT:
ORA-20015: Error
RESULT:
Thus the PL/SQL program for implementing Exception handling has been successfully executed.
10
EX.NO:8 DESIGN PL/SQL BLOCKS USING SUBPROGRAMS NAMELY
FUNCTIONS AND PROCEDURES
AIM:
To implement and execute Procedures in Oracle Database using Procedural Language
concepts.
PROCEDURES:
1) Procedure is a sub program used to perform an action.
2) Replace-recreates the procedure if it already exists.
3 MODES:
1) IN – Means you must specify a value for the argument at the time execution of
the procedure.
3) INOUT – When calling the procedure, yu must specify the value and that procedures
passes value back to the calling procedure
CREATING A PROCEDURE
SYNTAX:
BEGIN
greetings;
END;
11
DELETING A PROCEDURE
SYNTAX:
EXAMPLE:
Procedure dropped
EXAMPLE 2
BEGIN
INSERTUSER(101,'Rahul');
dbms_output.put_line('record inserted
successfully'); END;
PL/SQL FUNCTIONS
SYNTAX:
12
return number
is
n3 number(8);
begin
n3 :=n1+n2;
return n3;
end;
DECLARE
n3 number(2);
BEGIN
n3 := adder(11,22);
dbms_output.put_line('Addition is: ' || n3);
END;
Output:
Addition is: 33
Statement processed.
RESULT:
Thus the PL/SQL procedures and functions are executed successfully.
13