Procedures Functions Triggers
Procedures Functions Triggers
Procedures Functions Triggers
Procedure created.
Showing errors
Warning: Procedure created with compilation errors.
LINE/COL ERROR
-------- -----------------------------------------------------------------
7/1 PLS-00103: Encountered the symbol ";"
To execute Procedure
• Call procedure in a PL/SQL Program
DECLARE
sq number:=0;
x number:=&x;
BEGIN
squareNum(x,sq);
dbms_output.put_line(' Square is:'||sq);
END;
/ SQL> start F:\advdbms\2020\lab\program\procedure\call_sqr.sql;
Enter value for x: 4
old 3: x number:=&x;
new 3: x number:=4;
Square is:16
SQR
----------
64
Procedure with No Parameters
SET SERVEROUTPUT ON
CREATE OR REPLACE PROCEDURE DisplaySalary IS
temp_Salary NUMBER(10,2);
temp_name emp.ename%type;
BEGIN
SELECT Sal,ename INTO temp_Salary,temp_name FROM emp WHERE
empno=102;
DBMS_OUTPUT.PUT_LINE ('Salary of '||temp_name||' is
'||temp_salary);
END;
/
Executing DisplaySalary Procedure
Procedure created.
Procedure created
Calling DisplaySalary1
DECLARE
v_sal emp.sal%type;
v_eno emp.empno%type:=&v_eno;
BEGIN
displaysalary1(v_eno,v_sal);
dbms_output.put_line('Actual salary of employee '||v_eno||' is
'||v_sal);
END;
/
Executing
SQL> start F:\advdbms\2020\lab\program\procedure\call_dissal.sql;
Enter value for v_eno: 101
old 3: v_eno emp.empno%type:=&v_eno;
new 3: v_eno emp.empno%type:=101;
Actual salary of employee 101 is 30000
V_SAL
----------
55000
Procedure created.
DECLARE
v_sal emp.sal%type;
v_eno emp.empno%type:=&v_eno;
BEGIN
v_sal:=display_salary(v_eno);
dbms_output.put_line('Salary of '||v_eno||' is '||v_sal);
END;
/
Executing Functions
PL/SQL Block
DECLARE
v_sal emp.sal%type;
v_eno emp.empno%type:=&v_eno;
BEGIN
v_sal:=display_salary(v_eno);
dbms_output.put_line('Salary of '||v_eno||' is '||v_sal);
END;
/
SQL> start F:\advdbms\2020\lab\program\procedure\call_func.sql;
Enter value for v_eno: 103
old 3: v_eno emp.empno%type:=&v_eno;
new 3: v_eno emp.empno%type:=103;
Salary of 103 is 55000
Executing Functions
SELECT
DISPLAY_SALARY(103)
-------------------
55000
Executing Functions
EXECUTE/EXEC
Function created.
6 rows selected.
SQL> select display_ename('D2') from dual;
DISPLAY_ENAME('D2')
--------------------------------------------------------------------------------
Ravi
DISPLAY_ENAME('D1')
--------------------------------------------------------------------------------
Tina
Function Vs Procedure
Stored Procedure Function
May or may not returns a value to the calling Returns a value to the calling part of the
part of program. program.
Uses IN, OUT, IN OUT parameter. Uses only IN parameter.
Returns a value using “ OUT” parameter. Returns a value using “RETURN”.
Does not specify the datatype of the value if Necessarily specifies the datatype of the
it is going to return after a calling made to it. value which it is going to return after a
calling made to it.
Cannot be called from the function block of Can be called from the procedure block of
code. code.
DATABASE TRIGGERS
• Database trigger – a stored PL/SQL program unit that is
associated with a specific database table, or with certain
view types – can also be associated with a system event
such as database startup.
• Two sections:
• A named database event
• A PL/SQL block that will execute when the event occurs
INSERT Undefined – all column values Stores the values that will be
are NULL as there is no “old” inserted into the new row for the
version of the data row being table.
inserted.
UPDATE Stores the original values for the Stores the new values for the
row being updated before the row – values the row will
update takes place. contain after the update takes
place.
DELETE Stores the original values for the Undefined – all column values
row being deleted before the are NULL as there will not be a
deletion takes place. “new” version of the row being
deleted.
Example
Tutorial 8 – 01/04/2023
Convert ER to Schema