CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.

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.
Chapter 4B: More Advanced PL/SQL Programming
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
1 PL/SQL programming Procedures and Cursors Lecture 1 Akhtar Ali.
Cursors in Pl/SQL Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
Advanced Databases Advanced PL/SQL Programming: Procedure, Function and Package.
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.
Copyright © SUPINFO. All rights reserved Procedures and functions.
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.
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.
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.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
L/O/G/O Working with Composite Data Types. Objectives After completing this lesson, you should be able to do the following: –Create user-defined PL/SQL.
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.
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.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
Using SQL in PL/SQL ITEC 224 Database Programming.
Chapter Sixteen Cursors Objective: – Introduction to cursors – Use of cursors in a database record – Implicit & explicit cursors – Cursors & loops – Cursors.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
ITEC 224 Database Programming PL/SQL Lab Cursors.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
implicit and an explicit cursor
Creating Functions. V 12 NE - Oracle 2006 Overview of Stored Functions A function is a named PL/SQL block that returns a value A function can be stored.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
Creating Procedures. PL/SQL Program Construct Tools Constructs Anonymous Block Application procedures or functions Application packages Application Triggers.
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.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
6 Copyright © 2004, Oracle. All rights reserved. Working with Composite Data Types.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
STORED PROCEDURE & STORED FUNCTION Politeknik Telkom 2012.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
PL/SQL CURSORS.
ITEC 224 Database Programming
Creating Stored Functions
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Pl/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-3
Creating Stored Procedures
Interacting with the Oracle Server
Interacting with the Oracle Server
Creating Stored Procedures and Functions
Interacting with the Oracle Server
PL/SQL.
Collection in PL/SQL.
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Cursors ITEC 224 Database Programming PL/SQL Lab.
Database Management Systems 2
Database Management Systems 2
Database Management Systems 2
Working with Composite Datatypes
Handling Exceptions.
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Cursors.
Chapter 8 Advanced SQL.
PL/SQL Declaring Variables.
Presentation transcript:

CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross

Outline Working with Composite Datatype More on Cursor Exception Handling Creating Function/ Procedures/ Subprograms

Working with Composite Datatype Are of two types: PL/SQL RECORDS PL/SQL Collections index by Table Nested Table VARRAY Contain internal components Are reusable

PL/SQL RECORDS Where field_declaration is: Use dot notation Example Use dot notation emp_record.job_id ...

The %rowtype Attribute Declare a variable according to a collection of columns in a database table or view. Prefix %rowtype with the database table. Fields in the record take their names and data types from the columns of the table or view. DECLARE identifier reference%ROWTYPE; … emp_record employees%ROWTYPE;

Index by Tables An associative array (also called an index-by table) A set of key-value pairs. Each key is unique, and is used to locate the corresponding value. The key can be either an integer or a string. Using a key-value pair for the first time adds that pair to the associative array. Using the same key with a different value changes the value.

Index by Tables

Example SQL> DECLARE 2 -- Associative array indexed by string: 3 4 TYPE population IS TABLE OF NUMBER -- Associative array type 5 INDEX BY VARCHAR2(64); 6 7 city_population population; -- Associative array variable 8 i VARCHAR2(64); 9 10 BEGIN 11 -- Add new elements to associative array: 12 13 city_population('Smallville') := 2000; 14 city_population('Midland') := 750000; 15 city_population('Megalopolis') := 1000000; 16 17 -- Change value associated with key 'Smallville': 18 19 city_population('Smallville') := 2001; 20 21 -- Print associative array: 22 23 i := city_population.FIRST; 24 25 WHILE i IS NOT NULL LOOP 26 DBMS_Output.PUT_LINE 27 ('Population of ' || i || ' is ' || TO_CHAR(city_population(i))); 28 i := city_population.NEXT(i); 29 END LOOP; 30 END; 31 / Example

Using index by Table Methods

Index by Table of Records Define a table variable with a permitted PL/SQL data type. Declare a PL/SQL variable to hold department information.

Example DECLARE TYPE stud_table_type is table of student%ROWTYPE INDEX BY BINARY_INTEGER; my_stud_table stud_table_type; v_count NUMBER(9):= 550156548; BEGIN FOR i IN 550156548..v_count LOOP SELECT * INTO my_stud_table(i) FROM student WHERE snum = i; END LOOP; FOR i IN my_stud_table.FIRST..my_stud_table.LAST LOOP DBMS_OUTPUT.PUT_LINE(my_stud_table(i).sname); END;

Cursors Every SQL statement executed by the Oracle Server has an individual cursor associated with it: Implicit cursors: Declared for all DML and PL/SQL select statements Explicit cursors: Declared and named by the programmer

Implicit Cursor Oracle Engine explicitly opens a cursor PL/SQL Code Block on Client Machine Server Oracle Engine HDD Cursor Opened on the Server Rows retrieved Cursor Attributes: %ISOPEN, %FOUND, %NOTFOUND, %ROWCOUNT Usage: SQL%ROWCOUNT

Explicit Cursor Declare Create a named SQL area Open Use explicit cursors to individually process each row returned by a multiple-row SELECT statement. Declare Create a named SQL area Open Identify the Active Set Fetch Load the current row into variables Verify Check whether empty or not Close Release the active set if empty

Explicit Cursor Working Principle Server Oracle Engine HDD Open <CursorName> Fetch <CursorName> INTO … Client Machine Client RAM Fetch One row at a time Server RAM V1 V2 V3 V4 C1 C2 C3 C4 Active Data Set Advance the cursor

Declaring the Cursor Do not include the into clause in the cursor declaration. If processing rows in a specific sequence is required use the order by clause in the query. DECLARE CURSOR emp_cursor IS SELECT employee_id, FROM employees; CURSOR dept_cursor IS SELECT * FROM departments WHERE location_id = 170; BEGIN …

Opening the Cursor Open the cursor to execute the query and identify the active set. If the query returns no rows, no exception is raised. Use cursor attributes to test the outcome after a fetch. Dynamically allocates memory for a context area that eventually contains crucial processing information. Parses the SELECT statement. Binds the input variables—sets the value for the input variables by obtaining their memory addresses. Identifies the active set—the set of rows that satisfy the search criteria. Rows in the active set are not retrieved into variables when the OPEN statement is executed. Rather, the FETCH statement retrieves the rows. Positions the pointer just before the first row in the active set.

Fetching Data from the Cursor Retrieve the current row values into variables. Include the same number of variables. Match each variable to correspond to the columns positionally. Test to see whether the cursor contains rows.

Example SET SERVEROUTPUT ON DECLARE v_empno employees.employee_id%TYPE; v_ename employees.last_name%TYPE; CURSOR emp_cursor IS SELECT employee_id, last_name FROM employees; BEGIN OPEN emp_cursor; FOR i IN 1..10 LOOP FETCH emp_cursor INTO v_empno, v_ename; DBMS_OUTPUT.PUT_LINE (TO_CHAR(v_empno) II' 'II v_ename); END LOOP; END ;

Closing the Cursor Close the cursor after completing the processing of the rows. Reopen the cursor, if required. Do not attempt to fetch data from a cursor after it has been closed.

More Example

Example DECLARE CURSOR emp_cursor IS SELECT employee_id, last_name FROM employees; emp_record emp_cursor%ROWTYPE; BEGIN OPEN emp_cursor; LOOP FETCH emp_cursor INTO emp_record; EXIT WHEN emp_cursor%NOTFOUND; INSERT INTO temp_list (empid, empname) VALUES (emp_record.employee_id, emp_record.last_name); END LOOP; COMMIT; CLOSE emp_cursor; END;

Cursor for Loops The cursor for loop is a shortcut to process explicit cursors. Implicit open, fetch, exit, and close occur. The record is implicitly declared.

Example DECLARE CURSOR emp_cursor IS SELECT last_name, department_id FROM employees; BEGIN FOR emp_record IN emp_cursor LOOP —implicit open and implicit fetch occur IF emp_record.department_id = 80 THEN DBMS_OUTPUT.PUT_LINE ('Employee ' || emp_record.last_name || ' works in the Sales Dept. '); END IF; END LOOP; --implicit close and implicit loop exit END ;

DECLARE v_employee_id employees.employee_id%TYPE; v_job_id employees.job_id%TYPE; v_start_date DATE; v_end_date DATE; CURSOR emp_cursor IS SELECT employee_id, job_id, start_date, end_date FROM job_history ORDER BY employee_id; BEGIN OPEN emp_cursor; LOOP FETCH emp_cursor INTO v_employee_id, v_job_id, v_start_date, v_end_date; DBMS_OUTPUT.PUT_LINE ('Employee #: ' || v_employee_id || ' held the job of ' || v_job_id || ' FROM ' || v_start_date | | ' TO ' I I v_end_date); EXIT WHEN emp_cursor%ROWCOUNT > 4 OR emp_cursor%NOTFOUND; END LOOP; CLOSE emp_cursor; END;

Handling Exceptions with PL/SQL An Exception is an identifier in PL/SQL that is raised during execution. How is it raised? An Oracle error occurs. You raise it explicitly. How do you handle it? Trap it with a handler. Propagate it to the calling environment.

Handling Exception

Example Predefined Exceptions

User Defined Exception

PL/SQL Program Constructs

Block Structure for PL/SQL Subprograms - The PL/SQL subprogram type, that is, either a procedure or a function - The name of the subprogram - The parameter list, if one exists - The RETURN clause, which applies only to functions The IS or AS keyword is mandatory. The keyword DECLARE is not used

Procedure The replace option indicates that if the procedure exists, it will be dropped and replaced with the new version created by the statement. PL/SQL block starts with either begin or the declaration of local variables and ends with either end or end procedure_name. Type of argument: IN (default) , OUT , IN OUT

Formal Versus Actual Parameters CREATE PROCEDURE raise_sal (p_id NUMBER, p_amount NUMBER) … END raise_sal; raise_sal(v_id, 2000)

Procedure Parameter Model Attempts to change the value of an IN parameter will result in an error.

Creating Procedures with Parameters OUT IN OUT Default mode Must be specified Value is passed into subprogram Returned to calling environment Passed into subprogram; returned to calling Formal parameter acts as a constant Uninitialized variable Initialized variable Actual parameter can be a literal, expression, constant, or initialized variable Must be a variable Can be assigned a default value Cannot be assigned a default value

In, Out Parameters: Example

There must be at least one RETURN (expression) Functions The RETURN data type must not include a size specification. There must be at least one RETURN (expression)

CREATE OR REPLACE Function IncomeLevel ( name_in IN varchar2 ) RETURN varchar2 IS monthly_value number(6); ILevel varchar2(20); cursor c1 is SELECT SAL FROM scott.EMP WHERE ename = name_in; BEGIN open c1; fetch c1 into monthly_value; close c1; IF monthly_value <= 1000 THEN ILevel := 'Low Income'; ELSIF monthly_value > 1000 and monthly_value <= 2000 THEN ILevel := 'Avg Income'; ELSIF monthly_value > 2000 and monthly_value <= 3000 THEN ILevel := 'Moderate Income'; ELSE ILevel := 'High Income'; END IF; RETURN ILevel; END;

Locations to Call User-Defined Functions Select list of a select command Condition of the where and having clauses CONNECT BY, START WITH, ORDER BY, and GROUP by clauses values clause of the insert command set clause of the update command E.g. select empno, IncomeLevel(ename) as Income from scott.EMP where ename='KING'

Restrictions To be callable from SQL expressions, a user-defined function must: Be a stored function Accept only in parameters Accept only valid SQL data types, not PL/SQL specific types, as parameters Return data types that are valid SQL data types, not PL/SQL specific types Functions called from SQL expressions cannot contain DML statements.

Restrictions To be callable from SQL expressions, a user-defined function must: Functions called from update/delete statements on a table T cannot contain DML on the same table T. Functions called from a DML statement on a table T cannot query the same table. Functions called from SQL statements cannot contain statements that end the transactions. Calls to subprograms that break the previous restriction are not allowed in the function.

Comparing Procedures and Functions Execute as a PL/SQL statement Invoke as part of an expression No return clause in the header Must contain a return clause in the header Can return none, one, or many values Must return a single value Can contain a return statement Must contain at least one return statement

Thank you Next Lecture: Transactions