0% found this document useful (0 votes)
78 views13 pages

19CB408 - DBMS LAB Experiment 5 To 8

The document discusses implementing procedures and functions in PL/SQL. It provides the syntax for creating procedures with different argument modes like IN, OUT and INOUT. An example is given to create a procedure to insert records into a table. The syntax for creating functions is also provided along with an example to create and execute a function to add two numbers. Procedures are used to perform actions while functions return a value. Exception handling and triggers are also discussed with examples provided.

Uploaded by

PUGALENTHI M S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views13 pages

19CB408 - DBMS LAB Experiment 5 To 8

The document discusses implementing procedures and functions in PL/SQL. It provides the syntax for creating procedures with different argument modes like IN, OUT and INOUT. An example is given to create a procedure to insert records into a table. The syntax for creating functions is also provided along with an example to create and execute a function to add two numbers. Procedures are used to perform actions while functions return a value. Exception handling and triggers are also discussed with examples provided.

Uploaded by

PUGALENTHI M S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

EXP.

NO:5 IMPLEMENTATION OF CURSOR IN PL/SQL BLOCK

AIM:
To implement implicit and explicit cursors using PL/SQL programming

ALGORITHM:

Step 1: Declare the cursor for initializing the memory


Step 2: Opening the cursor for allocating the memory
Step 3: Fetching the cursor for retrieving the data
Step 4: Closing the cursor to release the allocated memory
Step 5: Execute the program

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);

select * from customers;

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:

The syntax for creating an explicit cursor is

CURSOR cursor_name IS select_statement;

Declaring the Cursor


CURSOR c_customers IS
SELECT id, name, address FROM
customers; Opening the Cursor
OPEN c_customers;
Fetching the Cursor
FETCH c_customers INTO c_id, c_name, c_addr;
Closing the Cursor
CLOSE c_customers;

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:

STEP1: Create a table emply101


STEP2: Insert values into the table
STEP3: Create another table emply102
STEP4: Create trigger
CREATE OR REPLACE TRIGGER <TRIGGER NAME>
{BEFORE/AFTER/INSTEAD OF}
{INSERT/UPDATE/DELETE}
ON <TABLENAME/VIEWNAME>
REFRENCECING {OLD AS OLD /NEW AS NEW}
[FOR EACH STATEMENT /FOR EACH ROW [WHEN <CONDITION>]]
STEP5: Insert and delete record into emply101
STEP6: View the updations in emply102

CREATE TABLE emply101


create table emply101(eid varchar(20),ename char(25), age number(10), salary
number(10));
insert into emply101 values(1,'abi',23,25000);
insert into emply101 values(2,'abinaya',22,20000);
insert into emply101 values(3,'abishek',23,25000);
select * from emply101;

CREATE TABLE emply102


create table emply102(eid varchar(20),ename char(25),age number(10),salary
number(10));
6
select * from emply102;
no data found
Creating trigger
create or replace trigger tfg1 before insert on emply101 for each row
begin
insert into emply102 values(:new.eid,:new.ename,:new.age,:new.salary);
end;

Insert values into emply101

insert into emply101 values(4,'bala',23,25000);


select * from emply101;

select * from emply102;

CREATING TRIGGER FOR UPDATING AND DELETING

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;

Update a value in emply101

update emply101 set salary=10000 where eid=1;


select * from emply102;

7
RESULT:
Thus the SQL triggers are executed successfully.

8
EX.NO:7 WRITE PL/SQL BLOCK PROGRAMS USING EXCEPTION HANDLING

AIM:

To implement exception handling using PL/SQL program

ALGORITHM:

Step 1: Define the declaration statements


Step 2: Define the Begin statements
Step 3: Define the exception statement using the syntax
WHEN exception THEN
statement;
Step 4: When an error occurs in the execution part then it will be shown in the exception part
Step 5: More than one exception statement can be declared

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.

2) OUT-passes back a value to its calling program.

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:

CREATE [OR REPLACE] PROCEDURE procedure_name


[(column_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
BEGIN
< procedure_body >
END procedure_name;

CREATE OR REPLACE PROCEDURE greetings


AS
BEGIN
dbms_output.put_line('Hello World!');
END;

To run this procedure

BEGIN
greetings;
END;

11
DELETING A PROCEDURE

SYNTAX:

DROP PROCEDURE procedure-name;

EXAMPLE:

DROP PROCEDURE greetings;

Procedure dropped

EXAMPLE 2

Create a table user1

create table user1(id number(10) primary key,name varchar2(100));

Create the procedure

create or replace procedure


INSERTUSER(id IN NUMBER, name IN VARCHAR2)
is
begin
insert into user1 values(id,name);
end;

To execute the procedure

BEGIN
INSERTUSER(101,'Rahul');
dbms_output.put_line('record inserted
successfully'); END;

PL/SQL FUNCTIONS

SYNTAX:

CREATE [OR REPLACE] FUNCTION function_name [parameters]


[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];

create or replace function adder(n1 in number, n2 in number)

12
return number
is
n3 number(8);
begin
n3 :=n1+n2;
return n3;
end;

EXECUTE THE FUNCTION

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

You might also like