DSC Chapter4 Updated
DSC Chapter4 Updated
Presented By :
Mrs. Samidha Chavan
Lecturer -Department of Information Tech.
Vidyalankar Polytechnic, Wadala (Mumbai)
Unit 4 : PL /SQL Programming
1.Introduction to PL/SQL
2.Control Structure
3.Exception handling
4.Cursor
5.Procedure
6.Function
7.Trigger
PL/SQL data types
PL/SQL data types
CHARACTER Data Type:
This data type basically stores alphanumeric characters in
string format.
This character data type is further classified as
follows:
• CHAR Data type (fixed string size)
• VARCHAR2 Data type (variable string size)
• VARCHAR Data type
• NCHAR (native fixed string size)
• NVARCHAR2 (native variable string size)
• LONG and LONG RAW
PL/SQL data types
NUMBER Data Type:
This data type stores fixed or floating point numbers up to 38 digits
of precision. This data type is used to work with fields which will
contain only number data.
1.DECLARE
2. a integer := 30;
3. b integer := 40;
4. c integer;
5. f real;
6.BEGIN
7. c := a + b; OUTPUT
8. dbms_output.put_line('Value of c: ' || c); Value of c: 70
9. f := 100.0/3.0; Value of f: 33.333333333333333333
10. dbms_output.put_line('Value of f: ' || f);
11.END; PL/SQL procedure successfully completed.
PL/SQL Constants
A constant is a value used in a PL/SQL block that remains unchanged
throughout the program.
1.DECLARE
2. -- constant declaration
3. pi constant number := 3.141592654;
4. -- other declarations
5. radius number(5,2);
6. dia number(5,2);
7. circumference number(7, 2);
8. area number (10, 2);
Exception Handling in PL/SQL
An exception is an error condition during a program execution.
PL/SQL supports programmers to catch such conditions
using EXCEPTION block in the program and an appropriate action is taken
against the error condition.
declare
v_sum number := 10;
v_divide number := 0;
v_result number;
begin
v_result := v_sum / v_divide;
dbms_output.put_line('v_result: '||v_result);
exception
when zero_divide then
dbms_output.put_line(‘ZERO_DIVIDE ERROR’);
end;
Output:
ZERO_DIVIDE ERROR
Example -System Defined Exception – NO_DATA_FOUND
OUTPUT
• PL/SQL allows you to define your own exceptions according to the need of your
program.
• A user-defined exception must be declared and then raised explicitly, using
either a RAISE statement
OR
procedure DBMS_STANDARD.RAISE_APPLICATION_ERROR.
1. Implicit cursors
2. Explicit cursors
Implicit Cursors
Output :
Trigger: A trigger is a stored procedure in database which
automatically invokes whenever a special event in the database
occurs. For example, a trigger can be invoked when a row is
inserted into a specified table or when certain table columns are
being updated.
Types of triggers
Row Triggers
A row trigger is fired each time the table is affected by the triggering
statement.
For example, if an UPDATE statement updates multiple rows of a table, a
row trigger is fired once for each row affected by the UPDATE statement.
Statement Triggers
A statement trigger is fired once on behalf of the triggering statement, regardless of the
number of rows in the table that the triggering statement affects, even if no rows are
affected.
For example, if a DELETE statement deletes several rows from a table, a statement-level
DELETE trigger is fired only once
BEFORE Triggers
BEFORE triggers run the trigger action before the triggering statement is run.
AFTER Triggers
AFTER triggers run the trigger action after the triggering statement is run.
INSTEAD OF Triggers
These triggers are called INSTEAD OF triggers because, unlike other types of triggers,
Oracle fires the trigger instead of executing the triggering statement.
Syntax for trigger creation :
Output
Create Function: Syntax
CREATE [OR REPLACE] FUNCTION function_name
[(argument1 [mode1] datatype1,
argument2 [mode2] datatype2,
. . .)]
RETURN datatype
IS|AS
function_body;
Procedure: Syntax
CREATE [OR REPLACE] PROCEDURE procedure_name
[(argument1 datatype1,
argument2 datatype2,
. . .)]
IS|AS
procedure_body
{
BEGIN
………
EXCEPTION
……….
END;
}
Q1. Write a function circle_area () to find area of circle, use
radius as input parameter.
DECLARE
c number (5,2);
BEGIN
c: = circle_area(1);
dbms_output.put_line('Area of circle: ' || c);
END;
Q2. Write a procedure emp_count ( ) to count number of employees in
department, use dept_no as input parameter (W-22)
CREATE OR REPLACE PROCEDURE emp_count(dept_no IN NUMBER)
IS
BEGIN
DECLARE
CURSOR c
IS SELECT * FROM emp where deptno=dept_no;
TYPE emp_tab IS TABLE OF c%ROWTYPE INDEX BY BINARY_INTEGER;
v_emp_tab emp_tab;
BEGIN
OPEN c;
FETCH c BULK COLLECT INTO v_emp_tab;
DBMS_OUTPUT.PUT_LINE(v_emp_tab.COUNT);
CLOSE c;
END;
END;
Q2. Write a procedure emp_count ( ) to count number of employees in
department, use dept_no as input parameter (W-22)
To execute procedure
exec emp_count(10);