PL SQL 2
PL SQL 2
PL/SQL Procedure
• The PL/SQL stored procedure or simply a procedure is a PL/SQL block which
performs one or more specific tasks.
The procedure contains a header and a body.
Header: The header contains the name of the procedure and the
parameters or variables passed to the procedure.
Body: The body contains a declaration section, execution section and
exception section similar to a general PL/SQL block.
Creating a Procedure
• A procedure is created with the CREATE OR REPLACE PROCEDURE statement.
The simplified syntax for the CREATE OR REPLACE PROCEDURE statement is as
follows −
Where,
procedure-name specifies the name of the
CREATE [OR REPLACE] PROCEDURE procedure.
[OR REPLACE] option allows the modification
procedure_name of an existing procedure.
The optional parameter list contains name,
[(parameter_name [IN | OUT | IN OUT] mode and types of the
type [, ...])] parameters. IN represents the value that will
be passed from outside and OUT represents
{IS | AS} the parameter that will be used to return a
BEGIN value outside of the procedure.
procedure-body contains the executable part.
< procedure_body > The AS keyword is used instead of the IS
END procedure_name; keyword for creating a standalone procedure.
Procedure Example:
CREATE OR REPLACE PROCEDURE
hello
AS
BEGIN
dbms_output.put_line('Hello
World!');
END;
begin
hello;
end;
Procedure Drop
begin
hello;
end;
Parameter Modes in PL/SQL Subprograms
DECLARE
n3 number(2);
BEGIN
n3 := adder(11,22);
dbms_output.put_line('Addition is: ' || n3);
END;
DECLARE
a number;
PL/SQL Function Example 2
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;
c := findMax(a, b);
dbms_output.put_line(' Maximum of (23,45): ' || c);
END;
Write PL/SQL procedure for an application using Find the total strength of
students in a class using functions (Labset A 11)
DECLARE
Create table customer(custid number, c number(2);
custname varchar(20), custaddress BEGIN
varchar(20) ); c := total_Customers();
dbms_output.put_line('Total no. of Customers: ' || c);
Insert into customer values(121,'Anu','Bangalore'); END;
insert into customer values(122,'Azar','Chennai');
insert into customer values(123, 'Ajay','Mysore');
insert into customer values(124, 'arun','kerala');
%NOTFOUND Its return value is TRUE if DML statements like INSERT, DELETE and UPDATE affect no
row, or a SELECT INTO statement return no rows. Otherwise it returns FALSE. It is a
just opposite of %FOUND.
%ISOPEN It always returns FALSE for implicit cursors, because the SQL cursor is automatically
closed after executing its associated SQL statements.
%ROWCOUNT It returns the number of rows affected by DML statements like INSERT, DELETE, and
UPDATE or returned by a SELECT INTO statement.
Write PL/SQL procedure for an application using cursors. (PART A 10)
Create table customers
DECLARE
(
total_rows number(2);
c_id number,
BEGIN
c_name varchar(20),
UPDATE customers
c_addr varchar(20),
SET c_salary = c_salary + 5000;
c_salary number(20)
IF sql%notfound THEN
);
dbms_output.put_line('no customers updated');
Insert into customers values(222,'Arun','Chennai',20000); ELSIF sql%found THEN
Insert into customers values(223,'Vara','Bangalore', 30000); total_rows := sql%rowcount;
Insert into customers values(224,'Zain','Mysore', 40000); dbms_output.put_line( total_rows || 'customers updated
Insert into customers values(225,'Dani','Chennai', 50000); ');
Insert into customers values(226,'Shoba','Kerala', 60000); END IF;
END;
select * from customers;
Write PL/SQL procedure for an application using cursors.
DECLARE
select * from customers; total_rows number(2);
BEGIN
UPDATE customers
SET c_salary = c_salary + 5000;
IF sql%notfound THEN
dbms_output.put_line('no customers updated');
ELSIF sql%found THEN
total_rows := sql%rowcount;
truncate table customers dbms_output.put_line( total_rows || 'customers
updated ');
END IF;
END;
Explicit Cursors
• The Explicit cursors are defined by the programmers to gain more control over
the context area. These cursors 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.
• Syntax of explicit cursor:
CURSOR cursor_name IS select_statement;
• Steps:
You must follow these steps while working with an explicit cursor.
1. Declare the cursor to initialize in the memory.
2. Open the cursor to allocate memory.
3. Fetch the cursor to retrieve data.
4. Close the cursor to release allocated memory.
Explicit Cursors DECLARE
c_id customers.id%type;
ID NAME AGE ADDR SALA c_name customers.name%type;
ESS RY c_addr customers.address%type;
1 Rames 23 Allahab 20000 CURSOR c_customers is
h ad SELECT id, name, address FROM customers;
2 Suresh 22 Kanpur 22000 BEGIN
OPEN c_customers;
3 Mahes 24 Ghazia 24000
h bad LOOP
FETCH c_customers into c_id, c_name, c_addr;
4 Chand 25 Noida 26000 EXIT WHEN c_customers%notfound;
an
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr
5 Alex 21 Paris 28000 );
6 Sunita 20 Delhi 30000 END LOOP;
CLOSE c_customers;
END;
%isopen Cursors declare
cursor c1 is select * from customer;
create table customer(id a customer%rowtype;
number(5), name varchar2(20)) begin
open c1;
if c1%isopen then
insert into customer values(101,'arun')
dbms_output.put_line('cursor is open');
insert into customer values(102,'ansh')
else
dbms_output.put_line('cursor is not
open');
end if;
close c1
end
%isopen Cursors
declare
cursor c1 is select * from customer;
a customer%rowtype;
begin
if c1%isopen then
dbms_output.put_line('cursor is open');
else
dbms_output.put_line('cursor is not
open');
end if;
end
PL/SQL Exception Handling Syntax:
DECLARE
• An error occurs during the <declarations section>