0% found this document useful (0 votes)
24 views

Procedure, Function, Trigger

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Procedure, Function, Trigger

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

PL/SQL

PROCEDURE
Procedure
• Procedure is a group of SQL statements which can be
executed repeatedly.
• It is very useful as it reduces the need for rewriting SQL
queries.

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.

• A procedure may or may not return any value.


Syntax for creating procedure:

CREATE OR REPLACE PROCEDURE


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

CREATE OR REPLACE PROCEDURE greetings


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

Output:
Procedure created.
Executing Stored Procedure
You can call procedure in two ways-

 Using the EXECUTE keyword


Ex- EXECUTE greetings;
Output-
Hello World!
PL/SQL procedure successfully completed.

Calling the name of the procedure from a PL/SQL block


 Ex-
BEGIN
Greetings();
END;
/
Output-
Hello World!
OUTPUT: Procedure created.
For calling the procedure created following code will
be executed:

OUTPUT:
Enter value for x: 10
Enter value for y: 20
Sum of two nos= 30
PL/SQL procedure successfully created.
Deleting Stored Procedure
• DROP is used to delete Procedure.

Syntax:
DROP PROCEDURE procedure_name;
Ex:
DROP PROCEDURE greetings;
PL/SQL Function

• The PL/SQL Function is very similar to PL/SQL


Procedure.
• The main difference between procedure and a
function is, a function must always return a
value, and on the other hand a procedure may or
may not return a value.
• Except this, all the other things of PL/SQL
procedure are true for PL/SQL function too.
Creating Function
CREATE OR REPLACE FUNCTION sum(a int, b int)
RETURN int IS
BEGIN
RETURN (a+b);
END;
/

OUTPUT –
Function Created.
Executing Function
BEGIN
dbms_output.put_line(‘Result is: ' || sum(50,90));
END;
/

OUTPUT –
Result is: 140
PL/SQL function successfully completed.
OUTPUT : Function created.
Deleting a Function
• DROP is used to delete function.

Syntax:
DROP FUNCTION function_name;
Ex:
DROP FUNCTION sum;
PL/SQL Trigger
• Triggers are stored programs, which are automatically executed or
fired when some event occurs.
• Trigger is invoked by Oracle engine automatically whenever a
specified event occurs. Trigger is stored into database and invoked
repeatedly, when specific condition match.

• Triggers are written to be executed in response to any of the


following events.
1. A database manipulation (DML) statement (DELETE, INSERT, or
UPDATE)
2. A database definition (DDL) statement (CREATE, ALTER, or DROP).
3. A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or
SHUTDOWN).

Triggers can be defined on the table, view, schema, or database with


which the event is associated.
Types Of Triggers In PL/SQL
The triggers can be categorized based on parameters. Types of
triggers are listed below:
1) Categorization on the trigger level.
a) ROW Level trigger: It fires for every record that got affected
with the execution of DML statements like INSERT, UPDATE,
DELETE etc.It always use a FOR EACH ROW clause in a
triggering statement.
b) STATEMENT Level trigger: It fires once for each statement
that is executed.

2) Categorization of the trigger timing.


c) BEFORE trigger: It fires before executing DML statement.
d) AFTER trigger: It fires after executing DML statement.
e) INSTEAD OF trigger: It is a special type of trigger and it gets
executed for each record that got updated by a DML
statement.
3) Categorization of the trigger event.

a) DML trigger: It fires with the execution of every DML


statement(INSERT, UPDATE, DELETE).
b) DDL trigger: It fires with the execution of every DDL
statement(CREATE, ALTER, DROP, TRUNCATE).
c) DATABASE trigger: It fires with the execution of every
database operation which can be LOGON, LOGOFF,
SHUTDOWN, SERVERERROR etc.
• 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
The following example shows how to create database trigger -

Assume two table,temp1 and temp2,having single column.


create table temp1 (describe varchar(50));
create table temp2 (describe varchar(50));

create or replace trigger ABC


before insert
on temp1
begin
insert into temp2 values('Test trigger');
end;
OUTPUT - Trigger created.

insert into temp1 values(‘Learning trigger');


select * from temp1; OUTPUT - Learning trigger
select * from temp2; OUTPUT - Trigger created
Deleting a Triggers

• Triggers can be deleted using DROP command.

Syntax:
DROP TRIGGER trigger_name;
Ex:
DROP TRIGGER abc;
The End !

You might also like