PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental.

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

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.
PL/SQL.
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.
PL_SQL Explanation of DISPCUST.SQL. 1 of 3 DECLARE cnum builder.customer.customer_id%type; cname builder.customer.customer_name%type; caddr builder.customer.customer_address%type;
Chapter 4B: More Advanced PL/SQL Programming
PL/SQL Continued. So far… Anonymous blocks Procedures Have you tried… –Downloading and running the dispcust.sql sample from your web page? –Writing your.
PL/SQL Agenda: Basic PL/SQL block structure
Introduction to PL/SQL
Bordoloi and Bock PL/SQL : INTRODUCTION. Bordoloi and BockPL/SQL PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Bordoloi and Bock EXCEPTIONS. Bordoloi and Bock Errors Two types of errors can be found in a program: compilation errors and runtime errors. There is.
1 Introduction to PL/SQL. 2  Procedural programming language  Uses detailed instructions  Processes statements sequentially  Combines SQL commands.
SQL enables us to create, organize, retrieve and maintain data stored in database it does not provide the features which a typical programming language.
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.
Oracle10g Developer: PL/SQL Programming1 Objectives Programming fundamentals The PL/SQL block Define and declare variables Initialize variables The NOT.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
PL/SQL : INTRODUCTION. PL/SQL PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database language. With PL/SQL, you.
Chapter 4: Introduction to PL/SQL
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
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.
PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
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 Application Development using PL/SQL Programming.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.
Handling Exceptions. 2 home back first prev next last What Will I Learn? Describe several advantages of including exception handling code in PL/SQL Describe.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
Using Oracle-Supplied Packages. 2 home back first prev next last What Will I Learn? Describe two common uses for the DBMS_OUTPUT server-supplied package.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Chapter 18: Exception Handling1 Chapter Eighteen Exception Handling Objective: – Define exceptions – List types of exception handlers – Trap errors – Exception.
Introduction to PL/SQL As usual, use speaker notes for additional information!
PL/SQL INTRODUCTION.
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.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
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.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
C LANGUAGE MULITPLE CHOICE QUESTION SET-2
PL/pgSQL
Interacting with the Oracle Server
Fundamentals of PL/SQL part 1 (Basics)
PL/SQL.
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
PL/SQL Package Week 8.
Handling Exceptions.
Sa0951a PL/SQL 1: Introduction
Chapter 4: Introduction to PL/SQL
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Handling Exceptions.
PL/SQL week10.
PL/SQL Declaring Variables.
Presentation transcript:

PL/SQL The Oracle Programming Language

Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental structures provided by PL/SQL –For sequence, selection, iteration –Variables, constants and data types –Assignment and other arithmetic statements –Customised error handling PL/SQL is a superset of the Data Manipulation Language part of SQL.

What is it? PL/SQL allows the user to store compiled code in the database, giving central access to routines associated with the data. They can be accessed –over the web, using the Web Application server –As form applications, under Developer/2000 –As embedded program logic in client-side applications –Using the SQL*Plus environment

Data Definition Language aspect PL/SQL does not provide for the Data Definition Language aspect of SQL. There is a package called dbms_sql that allows for data definition statements.

Scalar Data types Numeric –binary_integer integer from to 2 31 – 1 –natural integer from 0 to 2 31 –positive integer from 1 to 2 31 –Number(p,s) p is precision, s is scale Character –char(n) fixed length string of length n –varchar2(n) variable length string of maximum length n Other –Boolean boolean data type (true, false) –Date, time date Same as Oracle’s date.

Variables [not null] [:= ]; constant := E.g. i binary_integer; Cno number(5) not null:= 1111; Cname varchar2(30); Commission real(5,2):=12.5; Maxcolumns constant integer(2):=30; Hired_date date; Done boolean;

More on variables All variables with a ‘not null’ clause must be given an initial value. Any variable that is allowed a null and is not explicitly initialised is null. Anchored data types –A variable can be declared as of the same type as a database column: Stockvar builder.stock.stock_code%type Will declare a new variable stockvar that has the same type as the stock_code column in the stock table in the builder schema.

Program control statements Programs in a 3GL are made up of: –Comments and blank lines null; /*This is a block of comment Code, running over > 1 line*/ --This is a comment on a single line-- –Sequence Assignment, I/O, Arithmetic –Selection If-then, If-then-else, If-then-elseif –Iteration Loop

Sequence The assignment symbol is the same as that used in Pascal – a colon followed by an equals := This can be used for assignment and arithmetic: i := i+1; Cname := 'Jones'; sales := price * qty;

I/O dbms_output is a built-in package that allows the user to display information to the session output device (e.g. screen) as the program executes. The most commonly used procedures are: –put –put_line –new_line –get_line –get_lines –disable –enable

dbms_output As PL/SQL is essentially a back-end product, there is normally no need for screen output. However, for debugging purposes, we often need it. The dbms_output package works with a buffer into which information can be written: –Put, put_line, new_line The information can be subsequently retrieved: –get_line, get_lines

Turning on/off the dbms_package The package can be disabled: SQL> dbms_output.disable no dbms_output procedures will work except enable. This can also be done by entering: SQL> set serveroutput off Or enabled: SQL> dbms_output.enable( ) Or SQL> set serveroutput on size Where size is the buffer size. Default size is 2000 If you don’t specify the buffer size, the buffer may overflow.

put and put_line put puts data into the buffer, without an end of line marker: dbms_output.put(stock.stock_code); dbms_output.put(stock.stock_description); dbms_output.put(stock.unit_price); Put_line puts data into the buffer, with an end of line marker.

get_line Get_line Gets one line of information from the buffer, always in character string format. procedure get_line (line out varchar2, status out integer); The size of the varchar2 can be up to 255. See later for use of get_line(s)

Selection If-then statement: if(quantityrequired > stock_level) then dbms_output.putline(‘Stock level is only ’||Stock_level); End if; If-then-else statement: if(quantityrequired > stock_level) then dbms_output.putline(‘Stock level is only ’ ||Stock_level); Else dbms_output.putline(‘Now have’||(stock_level- quantityrequired)||’ items remaining’); stock_level = stock_level – quantityrequired; End if;

If-then-elseif This is the closest that PL/SQL comes to a case statement: If(sorderdate > delivereddate) then dbms_output.putline(‘The order cannot be delivered before it is ordered’); Elseif (sorderdate = delivereddate) then dbms_output.putline(‘The order delivery date is the same as the delivery date!’); Else dbms_output.putline (‘There were ‘||(deliverydate – sorderdate)||’ days between the order and the delivery’); End if;

Iteration There are three types of iterative statement: –Loop –For loop –While loop

loop The basic loop statement repeatedly executes until it finds an exit condition: Loop i:= i + 1; if i > 10 then exit; end if; sum := sum + 1; End loop;

for loop Syntax: for in [reverse].. loop ; End loop; Example: for i in loop dbms_output.put_line(‘i = ‘,||i); sum := sum + i; end loop;

While loop Syntax: While loop end loop; Example: i := 1; sum := 0; While (i < 1000) loop sum := sum + 1; i := 2 * i; end loop;

Anonymous blocks These are blocks of statements that can be run in a sequence. They can be anonymous or a subprogram. An anonymous block has the following structure:

Anonymous block structure Declare -- Declaration Section --Data, subprogram declarations begin -- Executable Section null; -- Program statements Exception --Exception section -- Exception handlers when others then null; -- default handler end;

Declarations The declaration section holds the declarations of –All variables and constants to be used in the scope of the block –Any subroutines that are local to the block –Any exception handler that we want to define –Any cursors we want to define (see later in course) The executable section holds –The main ‘program’ logic of the block. The exception section holds –Handlers for any exceptions that we can reasonably expect to occur during processing –A default exception handler to handle any exceptions that we haven’t foreseen.

Exceptions Exceptions are occurrences such as: –When we allocate stock to a customer order, if the stock falls below reorder level, we want to log the fact that the stock needs to be reordered. –When we try to allocate stock to a customer order, but there isn’t enough stock, we need to have a high level emergency warning for reordering.

Example The following procedure –Takes a customer id –Requests the customer details from the builder table customer –If successful Displays the customer details –Else Displays an error message. Calling it –It is called getcust.sql –It is stored on my C drive. –To call it, I open SQL*Plus SQL>start c:getcust

Code (1 of 3) DECLARE cnum builder.customer.customer_id%type; cname builder.customer.customer_name%type; caddr builder.customer.customer_address%type; status boolean; procedure get_cust_details ( cust_noin builder.customer.customer_id%type, cust_nameout builder.customer.customer_name%type, cust_addrout builder.customer.customer_address%type, statusoutboolean) is

Code(2 of 3) begin select builder.customer.customer_name, builder.customer.customer_address into cust_name, cust_addr from customer where builder.customer.customer_id = cnum; status := true; exception when no_data_found then status := false; end;

Code (3 of 3) begin cnum:=1; get_cust_details(cnum,cname, caddr, status); if (status) then dbms_output.put_line(cnum || ' ' || cname || ' ' || caddr); else dbms_output.put_line('Customer ' || cnum || ' not found'); end if; end;