0% found this document useful (0 votes)
24 views18 pages

4 - 2. PLSQL L5

PL/SQL is a combination of SQL and procedural programming. It allows developers to write blocks of executable code that include variable declarations, conditional logic, loops, and subprograms like functions and procedures. Functions return values while procedures perform actions. Triggers automatically run PL/SQL code in response to data changes or events in the database.

Uploaded by

vai123adr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views18 pages

4 - 2. PLSQL L5

PL/SQL is a combination of SQL and procedural programming. It allows developers to write blocks of executable code that include variable declarations, conditional logic, loops, and subprograms like functions and procedures. Functions return values while procedures perform actions. Triggers automatically run PL/SQL code in response to data changes or events in the database.

Uploaded by

vai123adr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

INTRODUCTION ON

PL/SQL
DBMS L-5
PL/SQL
PL/SQL is a combination of SQL along with the
procedural features of programming languages.

Lecture by Abir Shafy Bindu


PL/SQL BLOCK

DECLARE
<declarations section>
BEGIN DECLARE
message varchar2(20):= 'Hello, World!’;
<executable command(s)> BEGIN
dbms_output.put_line(message);
EXCEPTION END;
/

<exception handling>
END;

Lecture by Abir Shafy Bindu


Exception DECLARE
...

Handling BEGIN
...
EXCEPTION
WHEN <EXCEPTION_NAME1> THEN
Statement1 ;
WHEN <EXCEPTION_NAME2> THEN
Statement2 ;
...
WHEN OTHERS THEN
Default Statement ;
END ;
/

Lecture by Abir Shafy Bindu


CONDITIONAL LOGIC
DECLARE
JDATE DATE ;
YEARS NUMBER ;
BEGIN
--first retrieve hire_date and store the value into JDATE variable
SELECT HIRE_DATE INTO JDATE
FROM EMPLOYEES
WHERE EMPLOYEE_ID = 10000 ;
--calculate years from the hire_date field
YEARS := (MONTHS_BETWEEN(SYSDATE, JDATE) / 12) ;
IF YEARS >= 10 THEN
DBMS_OUTPUT.PUT_LINE('The employee worked 10 years or more') ;
ELSE
DBMS_OUTPUT.PUT_LINE('The employee worked less than 10 years') ;
END IF ;
EXCEPTION
--handle exceptions one by one here
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee is not present in database.') ;
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('I dont know what happened!') ;
END ;
/

Lecture by Abir Shafy Bindu


LOOP
DECLARE
i number(1);
j number(1);
BEGIN
<< outer_loop >>
FOR i IN 1..3 LOOP
<< inner_loop >>
FOR j IN 1..3 LOOP
dbms_output.put_line('i is: '|| i || ' and j is: ' || j);
END loop inner_loop;
END loop outer_loop;
END;
/

Lecture by Abir Shafy Bindu


Real life problem

Lecture by Abir Shafy Bindu


Lecture by Abir Shafy Bindu
Sub Program
• PL/SQL subprograms are named PL/SQL blocks that can be
invoked with a set of parameters. PL/SQL provides two kinds
of subprograms −
• Functions − These subprograms return a single value; mainly
used to compute and return a value.
• Procedures − These subprograms do not return a value
directly; mainly used to perform an action.

Lecture by Abir Shafy Bindu


PROCEDURE
CREATE OR REPLACE PROCEDURE IS_SENIOR_EMPLOYEE(EID IN VARCHAR2) IS
JDATE DATE ;
YEARS NUMBER ;
BEGIN
SELECT HIRE_DATE INTO JDATE
FROM EMPLOYEES
WHERE EMPLOYEE_ID = EID ;

YEARS := (MONTHS_BETWEEN(SYSDATE, JDATE) / 12) ;

IF YEARS >= 10 THEN


DBMS_OUTPUT.PUT_LINE('The employee worked 10 years or more') ;
ELSE
DBMS_OUTPUT.PUT_LINE('The employee worked less than 10 years') ;
END IF ;
END ;
/

Lecture by Abir Shafy Bindu


PROCEDURE

Lecture by Abir Shafy Bindu


FUNCTION
Syntax to create a function:
CREATE [OR REPLACE] FUNCTION function_name [parameters]
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];

Lecture by Abir Shafy Bindu


FUNCTION CREATE OR REPLACE FUNCTION GET_SENIOR_EMPLOYEE(EID
IN VARCHAR2)
RETURN VARCHAR2 IS
JDATE DATE ;
YEARS NUMBER ;
MSG VARCHAR2(100) ;
BEGIN
SELECT HIRE_DATE INTO JDATE FROM EMPLOYEES
WHERE EMPLOYEE_ID = EID ;
YEARS := (MONTHS_BETWEEN(SYSDATE, JDATE) / 12) ;
IF YEARS >= 10 THEN
MSG := 'The employee worked 10 years or more' ;
ELSE
MSG := 'The employee worked less than 10 years'
;
END IF ;
RETURN MSG ; --return the message

Lecture by Abir Shafy Bindu


FUNCTION EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 'No employee found.' ;
WHEN TOO_MANY_ROWS THEN
RETURN 'More than one employee found.' ;
WHEN OTHERS THEN
RETURN 'Some unknown error occurred.' ;
END ;
/

SELECT LAST_NAME,GET_SENIOR_EMPLOYEE(EMPLOYEE_ID)
FROM EMPLOYEES ;

Lecture by Abir Shafy Bindu


PL/SQL Trigger

Ø A trigger is a PL/SQL stored block. It is like a function or a procedure. However, it is


different than a function or a procedure. To run a function or a procedure, it is explicitly
called from a code. However, a trigger is automatically run by the Oracle. It is not called
from any code directly.
Ø A trigger is automatically run by Oracle -
Ø After/Before a database insertion operation
Ø After/Before a database deletion operation
Ø After/Before a database update operation
Ø After DDL operations, etc.

Lecture by Abir Shafy Bindu


PL/SQL CREATE [OR REPLACE ] TRIGGER trigger_name

Trigger
{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;

Lecture by Abir Shafy Bindu


Trigger CREATE OR REPLACE TRIGGER display_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/

Lecture by Abir Shafy Bindu


Thank You
Lecture by Abir Shafy Bindu

You might also like