03 PL SQL
03 PL SQL
PL/SQL
CONTENTS
• PL/SQL QUICK REVIEW
• PROCEDURES
• CURSORS
• TRIGGERS
• ERROR HANDLING
• SECURITY LOCKS
Introduction to PL/SQL
• What is PL/SQL?
• Executable Commands
– It’s enclosed between keywords BEGIN and END.
– It’s a mandatory section.
– It consists of executable PL/SQL statements of the program.
• Exception Handling
– This section starts with the keyword EXCEPTION.
– This section is also an optional section.
– It contains exceptions that handle errors in the program.
Simple PL/SQL program
Print natural numbers from 1 to 5.
declare
i number;
begin
i:=1;
loop
dbms_output.put_line(i);
i:=i+1;
exit when i > 5;
end loop;
end;
Procedures
• It’s a named block of statement.
• It may or may not return a value.
SYNTAX:
select name from student where marks=(select max(marks) fr
om student)
dbms_output.put_line(name);
end;
Executing a Procedure
CURSOR c_customers IS
SELECT id, name, address FROM customers;
Opening the Cursor
• OPEN c_customers;
Fetching the Cursor
• CLOSE c_customers;
Example
• 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;
• CLOSE c_customers;
• END;
• /
1 Ramesh Ahmedabad
2 Khilan Delhi
3 kaushik Kota
4 Chaitali Mumbai
5 Hardik Bhopal
6 Komal MP
• PL/SQL procedure successfully completed.
Implicit Cursor
• Implicit cursors get created when you execute
DML queries like Select, insert, delete, update.
• Oracle gives some useful attributes on this
implicit cursors to help us check the status of
DML operations
Attribute Usage
declare cursor c1
is
select deptName as Department,avg(fees) as Average_Fees from student
natural join department group by deptName;
rec1 c1%rowtype;
begin
for rec1 in c1 loop
dbms_output.put_line(rec1.Department ||' '||rec1.Average_Fees);
end loop;
end;
Bounded and unbounded cursors
• Bounded cursor bounds to a query
• cursor_name [ [NO] SCROLL ] CURSOR [( name
datatype, name data type, ...)]no FOR query;
• unbounded cursor variable is not bounded to
any query when it is declare it. So we have to
specify the query when we open it. See the
following example:
• OPEN my_cursor FOR SELECT * FROM city
WHERE counter = p_country;
Triggers
• Triggers are stored routines, which are
automatically executed when some events
occur.
• Triggers are written to be executed in response
to any of the following events:
• DML - DELETE, INSERT, or UPDATE.
• DDL - CREATE, ALTER, or DROP .
• A database operation (SERVERERROR, LOGON, LOGOFF,
STARTUP, or SHUTDOWN).
Trigger: General Syntax
CREATE [ OR REPLACE ] TRIGGER trigger_name
BEFORE/AFTER INSERT/UPDATE/DELETE
ON table_name [ FOR EACH ROW ]
DECLARE
-- variable declarations
BEGIN
-- trigger code
EXCEPTION WHEN ...
-- exception handling
END;
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
CREATE [OR REPLACE] TRIGGER trigger_name − Creates or replaces an existing
trigger with the trigger_name.
{BEFORE | AFTER | INSTEAD OF} − This specifies when the trigger will be executed.
The INSTEAD OF clause is used for creating trigger on a view.
{INSERT [OR] | UPDATE [OR] | DELETE} − This specifies the DML operation.
[OF col_name] − This specifies the column name that will be updated.
[ON table_name] − This specifies the name of the table associated with the trigger.
[REFERENCING OLD AS o NEW AS n] − This allows you to refer new and old values
for various DML statements, such as INSERT, UPDATE, and DELETE.
[FOR EACH ROW] − This specifies a row-level trigger, i.e., the trigger will be executed
for each row being affected. Otherwise the trigger will execute just once when the SQL
statement is executed, which is called a table level trigger.
WHEN (condition) − This provides a condition for rows for which the trigger would fire.
This clause is valid only for row-level triggers.
example