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.

Slides:



Advertisements
Similar presentations
Oracle Object-Relational Model. - Structures : tables, views, indexes, etc. - Operations : actions that manipulate data stored in structures - Integrity.
Advertisements

PL/SQL.
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.
INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Introduction to PL/SQL – Lecture 6.
Chapter 4B: More Advanced PL/SQL Programming
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.
Cursors in Pl/SQL Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
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.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
4 Copyright © 2004, Oracle. All rights reserved. Database Interfaces.
Cursor and Exception Handling By Nidhi Bhatnagar.
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:
SAGE Computing Services Customised Oracle Training Workshops and Consulting Are you making the most of PL/SQL? Hints and tricks and things you may have.
Copyright  Oracle Corporation, All rights reserved. 3 Creating Procedures.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
PL/SQL CSE2132 Database Systems Week 9 Lecture PL/SQL and Programming in Oracle - 2.
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 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
PL/SQL A BRIEF OVERVIEW DAVID WILSON. PL/SQL User’s Guide and Reference PL/SQL User’s Guide and Reference.
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 Cursors Session - II. Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount.
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.
1 CursorsCursors. 2 SQL Cursor A cursor is a private SQL work area. A cursor is a private SQL work area. There are two types of cursors: There are two.
PL/SQL Procedural Language / Structured Query Language.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/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.
PL/SQL Declaring Variables PL/SQL Block Structure DECLARE (Optional) Variables, cursors, user-defined exceptions BEGIN (Mandatory) - SQL statements -
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.
Copyright  Oracle Corporation, All rights reserved. 18 Interacting with the Oracle Server.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
implicit and an explicit cursor
COT3000 PL/SQL Week 8 Lecture PL/SQL and Programming in Oracle - 1
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.
RETRIEVE A NO. OF ROWS ¦ Declare a cursor ¦ Open the cursor ¦ Fetch rows of data ¦ Stop fetching rows ¦ Close the cursor.
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.
Database An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL Definition: PL/SQL.
What Are Subprograms? Subprograms are named PL/SQL blocks that can take parameters and be invoked. Subprograms allow decomposition of a program into logical.
Program with PL/SQL Lesson 4.
Interacting with the Oracle8 Server
Interacting with the Oracle Server
Interacting with the Oracle Server
Creating Stored Procedures and Functions
Interacting with the Oracle Server
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Writing Control Structures
SQL PL/SQL Presented by: Dr. Samir Tartir
Interacting with the Oracle Server
Writing Correlated Subqueries
Working with Composite Datatypes
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
PL/SQL week10.
Chapter 8 Advanced SQL.
PL/SQL Declaring Variables.
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

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 power of SQL with the data processing power of procedural languages.

Block Structure PL/SQL is a block-structured language. The basic units (procedures, functions, and anonymous blocks) that make up a PL/SQL program are logical blocks, which can contain any number of nested sub- blocks.

Logical Blocks Each logical block corresponds to a problem or subproblem to be solved. A block (or sub-block) lets you group logically related declarations and statements. The declarations are local to the block and cease to exist when the block completes.

Declarative First comes the declarative part, in which items can be declared. Once declared, items can be manipulated in the executable part.

Nesting You can nest sub-blocks in the executable and exception-handling parts of a PL/SQL block or subprogram but NOT in the declarative part.

Image

Variables and Constants PL/SQL lets you declare constants and variables, then use them in SQL and procedural statements anywhere an expression can be used.

SQL Datatypes Variables can have any SQL datatype, such as CHAR, DATE, or NUMBER, or any PL/SQL datatype, such as BOOLEAN or BINARY_INTEGER

Assigning Values You can assign values to a variable in three ways. The first way uses the assignment operator (:=), a colon followed by an equal sign. Examples: tax := price * tax_rate; valid_id := FALSE;

2 nd Way to Assign Values The second way to assign values to a variable is by selecting (or fetching) database values into it. Example: (Compute a 10% bonus when you select the salary of an employee. ) SELECT sal * 0.10 INTO bonus FROM emp WHERE empno = emp_id;

3 rd Way to Assign a Value The third way to assign values to a variable is by passing it as an OUT or IN OUT parameter to a subprogram. Example: DECLARE my_sal REAL(7,2); PROCEDURE adjust_salary (emp_id INT, salary IN OUT REAL) IS... BEGIN SELECT AVG(sal) INTO my_sal FROM emp; adjust_salary(7788, my_sal); -- assigns a new value to my_sal

Declaring Constants Declaring a constant is like declaring a variable except that you must add the keyword CONSTANT and immediately assign a value to the constant. Thereafter, no more assignments to the constant are allowed. Example: credit_limit CONSTANT REAL := ;

Cursors Oracle uses work areas to execute SQL statements and store processing information.

2 types of Cursors A PL/SQL construct called a cursor lets you name a work area and access its stored information. There are two kinds of cursors: implicit and explicit.

Implicit Cursor PL/SQL implicitly declares a cursor for all SQL data manipulation statements, including queries that return only one row.

Explicit Cursor For queries that return more than one row, you can explicitly declare a cursor to process the rows individually. Example: DECLARE CURSOR c1 IS SELECT empno, ename, job FROM emp WHERE deptno = 20;

Cursor For Loops A cursor FOR loop implicitly declares its loop index as a record that represents a row fetched from the database. DECLARE CURSOR c1 IS SELECT ename, sal, hiredate, deptno FROM emp;

Fetch Next, it opens a cursor, repeatedly fetches rows of values from the result set into fields in the record. BEGIN FOR emp_rec IN c1 LOOP... salary_total := salary_total + emp_rec.sal;

Closes Loop Then closes the cursor when all rows have been processed. END LOOP;

Full Example of Cursor For Loop DECLARE CURSOR c1 IS SELECT ename, sal, hiredate, deptno FROM emp;... BEGIN FOR emp_rec IN c1 LOOP... salary_total := salary_total + emp_rec.sal; END LOOP;

Cursor Variable A cursor variable points to the current row in the result set of a multi-row query. Unlike a cursor, a cursor variable can be opened for any type-compatible query. It is not tied to a specific query. Cursor variables are true PL/SQL variables, to which you can assign new values and which you can pass to subprograms stored in an Oracle database.

Example for Cursor Variable The following procedure opens the cursor variable generic_cv for the chosen query: PROCEDURE open_cv (generic_cv IN OUT GenericCurTyp,choice NUMBER) IS BEGIN IF choice = 1 THEN OPEN generic_cv FOR SELECT * FROM emp; ELSIF choice = 2 THEN OPEN generic_cv FOR SELECT * FROM dept; ELSIF choice = 3 THEN OPEN generic_cv FOR SELECT * FROM salgrade; END IF;... END;

The program below processes an order for a tennis racket.

DECLARE First, it declares a variable of type NUMBER to store the quantity of tennis rackets on hand. DECLARE qty_on_hand NUMBER(5);

Then, it retrieves the quantity on hand from a database table named inventory. BEGIN SELECT quantity INTO qty_on_hand FROM inventory WHERE product = 'TENNIS RACKET' FOR UPDATE OF quantity;

(condition) IF THEN If the quantity is greater than zero, the program updates the table and inserts a purchase record into another table named purchase_record. IF qty_on_hand > 0 THEN -- check quantity UPDATE inventory SET quantity = quantity – 1 WHERE product = 'TENNIS RACKET'; INSERT INTO purchase_record VALUES ('Tennis racket purchased', SYSDATE);

ELSE Otherwise, the program inserts an out-of- stock record into the purchase_record table. ELSE INSERT INTO purchase_record VALUES ('Out of tennis rackets', SYSDATE); END IF; COMMIT; END;