Oracle PL/SQL Eyad Husni Elshami. Why PL/SQL Block Structures: – PL/SQL consists of blocks of code, which can be nested within each other. Each block.

Slides:



Advertisements
Similar presentations
PL/SQL.
Advertisements

BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
Chapter 4B: More Advanced PL/SQL Programming
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Programming in Oracle with PL/SQL
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
Cursors in Pl/SQL Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
PL/SQL Bulk Collections in Oracle 9i and 10g Kent Crotty Burleson Consulting October 13, 2006.
Dr. James Dullea, CSC8490 Introduction to PL/SQLSlide 1 of 36 7From Prof. Dullea CSC8490 Introduction to PL/SQL Module 01-9 Revised: June 12, 2005 Dr.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
Distributed Database Applications COSC 5050 Week Three.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
Program with PL/SQL. Interacting with the Oracle Server.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
Advanced SQL: Cursors & Stored Procedures
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
Oracle PL/SQL. PL/SQL Originally modeled after ADA Originally modeled after ADA Created for Dept. of DefenseCreated for Dept. of Defense Allows expanded.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Using SQL in PL/SQL ITEC 224 Database Programming.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
ITEC 224 Database Programming PL/SQL Lab Cursors.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
implicit and an explicit cursor
PL/SQL. What is PL/SQL  Procedural Language – SQL  An extension to SQL with design features of programming languages (procedural and object oriented)
A procedure is a module performing one or more actions; it does not need to return any values. The syntax for creating a procedure is as follows: CREATE.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Introduction to PL/SQL Francis Thottungal. The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables,
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
PL/SQL  PL/SQL stands for Procedural Language/SQL.  PL/SQL extends SQL by adding constructs found in procedural languages like procedures, loops, variables,
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Program with PL/SQL Lesson 4.
Programming in Oracle with PL/SQL
Difference between Oracle PL/SQL and MySQL
Interacting with the Oracle Server
PL/SQL.
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Agenda Summary of last class Cursors Loops Records Simple Loops
PL/SQL Scripting in Oracle:
PL/SQL Programing : Triggers
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Chapter 8 Advanced SQL.
PL/SQL Declaring Variables.
Chapter 8 Advanced SQL Pearson Education © 2009.
Presentation transcript:

Oracle PL/SQL Eyad Husni Elshami

Why PL/SQL Block Structures: – PL/SQL consists of blocks of code, which can be nested within each other. Each block forms a unit of a task or a logical module. PL/SQL Blocks can be stored in the database and reused. Procedural Language Capability: – PL/SQL consists of procedural language constructs such as conditional statements (if else statements) and loops like (FOR loops). Better Performance: – PL/SQL engine processes multiple SQL statements simultaneously as a single block, thereby reducing network traffic. Error Handling: – PL/SQL handles errors or exceptions effectively during the execution of a PL/SQL program. Once an exception is caught, specific actions can be taken depending upon the type of the exception or it can be displayed to the user with a message.

Basic Structure of PL/SQL DECLARE /* Declarative section: variables, types, and local subprograms. It is an optional section*/ BEGIN /* Executable section: procedural and SQL statements go here. */ /* This is the only section of the block that is required. It is an mandatorysection */ EXCEPTION /* Exception handling section: error handling statements go here. It is an optional section */ END;

PL/SQL Variables and Types Information is transmitted between a PL/SQL program and the database through variables. Every variable has a specific type associated with it. That type can be one of the types used by SQL for database columns. – A generic type used in PL/SQL such as NUMBER, CHAR, DATE, …etc – Declared to be the same as the type of some database column

PL/SQL Variables and Types Declar Std_id number(2); Std_name Students.sname%TYPE; Std_record Students%ROWTYPE; Begin select sid into std_id from students where sname=‘ALI’; select sname into std_name from students where sid=12345; select * into std_record from students where sid=12345; End

PL/SQL Control Structures Conditional statement: – IF Statement Iterative Statements: – Simple Loop – While Loop – For Loop

PL/SQL Conditional statement IF condition THEN Statements ELSIF condition THEN Statements ELSE Statements END IF; Declare day char(3):= to_char(sysdate,'DY'); Begin If day = 'FRI' then DBMS_OUTPUT.PUT_LINE('Week end'); Elsif day = 'SAT' then DBMS_OUTPUT.PUT_LINE('New work week'); Else DBMS_OUTPUT.PUT_LINE('have a nice day'); End if; End; /

PL/SQL Iterative Statements Simple loop syntax: LOOP ; EXIT; /* EXIT WHEN condition;*/ END LOOP; DECLARE i NUMBER := 1; BEGIN LOOP INSERT INTO T1 VALUES(i,i); i := i+1; EXIT WHEN i>100; END LOOP; END;

PL/SQL Iterative Statements While loop syntax: WHILE condition LOOP ; END LOOP; DECLARE i NUMBER := 1; BEGIN WHILE i<100 LOOP INSERT INTO T1 VALUES(i,i); i := i+1; END LOOP; END;

PL/SQL Iterative Statements For loop syntax: FOR counter IN val1..val2 LOOP ; END LOOP; DECLARE i NUMBER := 1; BEGIN FOR i IN LOOP INSERT INTO T1 VALUES(i,i); END LOOP; END;

PL/SQL Cursors What are Cursors? – A cursor is a temporary work area created in the system memory when a SQL statement is executed. – A cursor contains information on a select statement and the rows of data accessed by it. This temporary work area is used to store the data retrieved from the database, and manipulate this data. – A cursor can hold more than one row, but can process only one row at a time. The set of rows the cursor holds is called the active set.

PL/SQL Implicit Cursors: When you execute DML statements or SELECT statements, implicit cursors are created to process these statements, there are attributes for each cursor: – %FOUND: Return TRUE value if DML statement affect at least one row or the select statement return at least on (example SQL%FOUND) – %NOTFOUND: Return FLASE value if DML statement affect at least one row or the select statement return at least on (example SQL%NOTFOUND) – %ROWCOUNT: Return the number of rows affected by the DML/SELECT statement (example SQL%ROWCOUNT) – %ISOPEN: Return TRUE value if the cursor is already open in the program.

PL/SQL Implicit Cursors: DECLARE rows_count number(5); BEGIN UPDATE students SET saverage= 40 where saverage is null ; IF SQL%NOTFOUND THEN dbms_output.put_line('None of the averages where updated'); ELSIF SQL%FOUND THEN rows_count := SQL%ROWCOUNT; dbms_output.put_line(‘Averages for ' || rows_count|| ‘ students are updated'); END IF; END;

PL/SQL Explicit Cursors An explicit cursor is defined in the declaration section of the PL/SQL Block. It is created on a SELECT Statement which returns more than one row. There are four steps in using an Explicit Cursor. – DECLARE the cursor in the declaration section. – OPEN the cursor in the Execution Section. – FETCH the data from cursor into PL/SQL variables or records in the Execution Section. – CLOSE the cursor in the Execution Section before you end the PL/SQL Block.

PL/SQL Explicit Cursors DECLARE CURSOR std_cur IS SELECT sid, sname, saverage FROM students; std_rec std_cur%rowtype; BEGIN IF NOT std_cur%ISOPEN THEN OPEN std_cur; END IF; LOOP FETCH std_cur INTO std_rec; EXIT WHEN std_cur%NOTFOUND; dbms_output.put_line(std_rec.sid || ' ' ||std_rec.sname || ' ' ||std_rec.saverage+5); END LOOP; END;

PL/SQL Procedures A stored procedure or in simple a procedure is a named PL/SQL block which performs one or more specific task. This is similar to a procedure in other programming languages. A procedure is similar to an anonymous PL/SQL Block but it is named for repeated usage. We can pass parameters to procedures in three ways: 1) IN-parameters 2) OUT-parameters 3) IN OUT-parameters

PL/SQL Procedures CREATE OR REPLACE PROCEDURE update_name IS BEGIN UPDATE students SET sname = UPPER(sname); UPDATE collge SET cname = UPPER(cname); COMMIT; END;

PL/SQL Procedures CREATE OR REPLACE PROCEDURE upadet_average (push_value IN NUMBER) IS BEGIN UPDATE students SET saverage = saverage +push_value WHERE saverage BETWEEN 55 and 59; COMMIT; END;

PL/SQL Procedures CREATE OR REPLACE PROCEDURE student_details IS CURSOR std_cur IS SELECT sname, cid, saverage FROM students; std_rec std_cur%rowtype; BEGIN FOR std_rec in std_cur LOOP dbms_output.put_line(std_cur.sname || ' ' ||std_cur.cid|| ' ' ||std_cur.saverage); END LOOP; END;

PL/SQL Procedures Execute procedurename;

PL/SQL Functions A function is a named PL/SQL Block which is similar to a procedure. The major difference between a procedure and a function is, a function must always return a value, but a procedure may or may not return a value.

PL/SQL Functions CREATE OR REPLACE FUNCTION need_to_pass (std_id IN NUMBER) RETURN NUMBER IS point NUMBER; BEGIN SELECT SAVERAGE INTO point FROM students WHERE sid = std_id; IF (point<60) THEN RETURN (60-point); ELSE RETURN 0; END IF; END;

PL/SQL Functions Execution for function like: 1.Since a function returns a value we can assign it to a variable. X := need_to_pass(12345); 2.As a part of a SELECT statement SELECT sname,saverage, need_to_pass(sid) FROM STUDENTS; 3.In a PL/SQL Statements like, dbms_output.put_line(need_to_pass(sid));

PL/SQL Triggers A trigger is a pl/sql block structure which is fired when a DML statements like Insert, Delete, Update is executed on a database table. CREATE OR REPLACE TRIGGER trigger_name {BEFORE | AFTER | INSTEAD OF } {INSERT | UPDATE | DELETE} [OF col_name] ON table_name [REFERENCING OLD AS o NEW AS n] [FOR EACH ROW] WHEN (condition) BEGIN --- sql statements END;

PL/SQL Triggers CREATE or REPLACE TRIGGER saverage_history BEFORE UPDATE OF saverage ON stdudents FOR EACH ROW /* WHEN :new.saverage>90*/ BEGIN INSERT INTO archive_average VALUES (:old.sid, :old.saverage, :new.saverage, sysdate, user); END;

PL/SQL Exception Handling We’ll study it later

create function grade(stdid in number) return char is savg stduents.saverage%type; Begin select saverage into asvg from stduents where sid= stdid; if savg>=65 then return 'A‘; Else Return ‘B’; End if; End;