DBMS Lab- 8 PLSQL-2
DBMS Lab- 8 PLSQL-2
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] ];
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.
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;
/
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
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