PLSQL Select & Cursors
PLSQL SELECT STATEMENT
Syntax:
SELECT[DISTINCT] <select_list>
INTO <variable_list>|<record_name>
FROM <table_list>
[WHERE <conditions>]
[GROUP BY <group_by_list>]
[HAVING <search_conditions>]
[ORDER BY <order_list> [ASC | DESC] ];
• Should return a single row value
• Should return a row
• Number of columns should match with number of variables
• Data types of columns should match with the data type of the variables.
PLSQL data types:
%TYPE : v_sname Sailors.sname%type
%ROWTYPE : rec_sailors Sailors%rowtype
Examples:
1. Program to illustrate PLSQL Select statement (column type)
Declare
V_sid Sailors.sid%type:=&sid;
V_sname Sailors.sname%type;
V_rating Sailors.rating%type;
V_age Sailors.age%type;
Begin
Select sid,sname,rating,age
Into V_sid,V_sname,V_rating,V_age
From Sailors
Where sid=V_sid;
Dbms_output.put_line ('Sailor_id: '||V_sid);
Dbms_output.put_line ('Sailor_name: '||V_sname);
Dbms_output.put_line ('Sailor_rating: '||V_rating);
Dbms_output.put_line ('Sailor_age: '||V_age);
End;
/
2. Program to illustrate PLSQL Select statement (row type)
Declare
V_sid Sailors.sid%type:=&sid;
Rec_sailors Sailors%rowtype;
Begin
Select *
Into Rec_sailors
From Sailors
Where sid=V_sid;
Dbms_output.put_line ('Sailor_id: '||V_sid);
Dbms_output.put_line ('Sailor_name: '||Rec_sailors.sname);
Dbms_output.put_line ('Sailor_rating: '||Rec_sailors.rating);
Dbms_output.put_line ('Sailor_age: '||Rec_sailors.age);
End;
/
PLSQL -CURSORS
Cursor is a work area to store multiple records from the database table.
In PLSQL whenever select statement returns more than one row, a cursor is used.
Implicit cursor: defined by system after every DML or PLSQL select statement execution
Explicit cursor: defined by users.
Explicit Cursor has 4 parts:
Cursor declaration - - in the declaration section
Opening Cursor - - - in the begin section
Reading Cursor- - - in the begin section
Closing Cursor- - - in the begin section
Syntax:
Cursor declaration: CURSOR <cursor_name>
IS
SELECT STATEMENT;
Example: CURSOR sail_cur
IS
Select * from Sailors;
Opening Cursor: OPEN <cursor_name>;
Example: OPEN sail_cur;
Reading Cursor: FETCH <cursor_name>
INTO <variable(s)>;
Example: FETCH sail_cur
INTO rec_sail;
(rec_sail sail_cur%rowtype)
Closing Cursor: CLOSE <cursor_name>;
Example: CLOSE sail_cur;
Cursor attributes:
%FOUND
%NOTFOUND
%ISOPEN
%ROWCOUNT
1. Program to display all sailors from sailors table (Explicit Cursor)
Declare
Cursor sail_cur
IS
Select * from Sailors;
Rec_sail Sailors%rowtype;
Begin
Open sail_cur;
Loop
Fetch sail_cur into Rec_sail;
Exit when sail_cur%NOTFOUND;
Dbms_output.put_line (sail_cur%rowcount||','||Rec_sail.sname||','||Rec_sail.rating||','||Rec_sail.age);
End Loop;
Close sail_cur;
End;
/
2. Program to display all boats from boats table (Cursor For Loop)
Declare
Cursor boat_cur
IS
Select bname,color from Boats;
Begin
For Rec_boat in boat_cur
Loop
Dbms_output.put_line (boat_cur%rowcount||','||Rec_boat.bname||','||Rec_boat.color);
End Loop;
End;
/
4. Program to implement implicit cursor
Begin
Update Sailors
Set rating=rating+1
Where age>40;
If SQL%FOUND
THEN
Dbms_output.put_line (SQL%ROWCOUNT|| ' rows updated');
ELSE
Dbms_output.put_line ('Age group not found');
End If;
End;
/
Exercise: Write a PLSQL Select which reads a sid from user and gets the corresponding
sailor row into a record, use implicit cursor to display a message if no row is returned
for the given sid.
5. Program to implement implicit cursor
Declare
Rec_sail Sailors%rowtype;
Begin
SELECT * into Rec_sail
FROM Sailors
WHERE sid=&sid;
If SQL%FOUND
THEN
Dbms_output.put_line (Rec_sail.sname||','||Rec_sail.age);
ELSE
Dbms_output.put_line ('Sailor not found');
End If;
End;
/
6. Program to illustrate Cursor parameters
Declare
Cursor sail_reserves(p_sid number)
IS
Select s.sname,r.bid,r.day
from Sailors S, Reserves r
where s.sid=r.sid
and r.sid = p_sid;
Rec_sail sail_reserves%rowtype;
Begin
Open sail_reserves(22);
Loop
Fetch sail_reserves into Rec_sail;
Exit when sail_reserves %NOTFOUND;
Dbms_output.put_line (Rec_sail.sname||','||Rec_sail.bid||','||Rec_sail.day);
End Loop;
Close sail_reserves;
Open sail_reserves(64);
Loop
Fetch sail_reserves into Rec_sail;
Exit when sail_reserves %NOTFOUND;
Dbms_output.put_line (Rec_sail.sname||','||Rec_sail.bid||','||Rec_sail.day);
End Loop;
Close sail_reserves;
End;
/
PLSQL – Subprograms
Stored Functions
Syntax:
CREATE [OR REPLACE] FUNCTION <function-name>
[arguments] RETURN <datatype>
IS [<local-variables>;]
BEGIN
<statements>;
END [<function-name>];
Stored Procedures
Syntax:
CREATE [OR REPLACE] PROCEDURE <function-name>
[arguments]
IS [<local-variables>;]
BEGIN
<statements>;
END [<procedure-name>];
Functions Examples:
1. Create or replace function pi
return number
is
begin
return round(22/7,2);
end;
/
i)declare
v_no number;
begin
v_no:=pi;
dbms_output.put_line(v_no);
End;
/
ii)begin
dbms_output.put_line(pi);
end;
/
2. Create or replace function pi_new
return number
is v_pi number(3,2);
begin
v_pi:=22/7;
return v_pi;
end;
/
3. Create or replace function num_add(p_no1 number,p_no2 number)
return number
is
v_add number(4);
begin
v_add:=p_no1+p_no2;
return v_add;
end;
/
select num_add(3,4) from dual
/
4. Create or replace function num_type(p_no number)
return varchar2
is
begin
if p_no>0
then return 'positive';
elsif p_no<0
then return 'negative';
else return 'zero';
end if;
end;
/
select num_type(9) from dual
/
Argument Types:
IN (Default) : A value cannot be assigned by the function to a parameter of type
IN, it has to be passed as an argument while calling the function
OUT: A value can be assigned by the function to a parameter of type OUT, but, it
cannot be passed as an argument while calling the function
IN OUT: It is a combination of both IN and OUT, a parameter of type IN OUT can
be
assigned a value in the functions, and can also be passed as an argument during
function call
5. create or replace function sample(a number, b number, c out number)
return number
is begin
c:=a-b;
return a+b;
end;
/
Declare
x number;
z number;
Begin
z:=sample(20,10,x);
dbms_output.put_line(x||' '||z);
end;
/
Procedure Examples:
Create or replace procedure edit_rating(p_sid number, p_rating number) is
Begin
update Sailors set rating=p_rating where sid=p_sid;
if SQL%found then
dbms_output.put_line(SQl%rowcount||' '||'rows updated');
else
dbms_output.put_line('no records found');
end if;
End;
/
execute edit_rating(22,10);
Viva Questions
1)What is the difference between a function and a stored procedure?
2) Write the process for creating and using explicit cursors in PLSQL
3) What is returned by the cursor attribute SQL%NOTFOUND?
4) What is returned by the cursor attribute SQL%FOUND?
5) What is the purpose of %type and %rowtype in PLSQL
6) What do you understand by PL/SQL cursors?
7)List the characteristics of PL/SQL?
8) What are COMMIT, ROLLBACK, and SAVEPOINT?
9) What is a Transaction?
10) When do we go for normalizing a relation?
References
1. https://www.youtube.com/watch?v=_snAMq
CBitg
2. https://www.guru99.com/pl-sql-cursor.html
http://www2.cs.uh.edu/~ceick/6340/lab/
3. https://blogs.oracle.com/oraclemagazine/
Labs/Lab8/cursors.htm
working-with-cursors