PL/SQL User Defined Types Record and Table Please use speaker notes for additional information!

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

Oracle 10g & 11g for Dev Virtual Columns DML error logging
Advanced SQL Topics Edward Wu.
Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL5.
© Abdou Illia MIS Spring 2014
Oracle Object-Relational Model. - Structures : tables, views, indexes, etc. - Operations : actions that manipulate data stored in structures - Integrity.
8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using Explicit Cursors.
Creating an Oracle Database Table Additional information is available in speaker notes!
PL/SQL.
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
H.Melikian1 PLSQL Dr.Hayk Melikyan Departmen of Mathematics and CS
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 1.
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.
Reports Using SQL Script Please check speaker notes for additional information!
April 14, 2004 Next Information Systems 1 Bulk Collections and Inserts in Oracle 9i & 10g Simay Alpöge Next Information Systems, Inc.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
PL/SQL.
Group functions using SQL Additional information in speaker notes!
Relational example using donor, donation and drive tables For additional information see the speaker notes!
PL/SQL - Using IF statements Please use speaker notes for additional information!
Chapter 8 Embedded SQL.
More on IF statements Use speaker notes for additional information!
Chapter 4B: More Advanced PL/SQL Programming
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
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.
SQL Use of Functions Character functions Please use speaker notes for additional information!
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.
Cursors in PL/SQL Includes cursor example and continuation of first cursor example Please use speaker notes for additional information!
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
1 Introduction to PL/SQL. 2  Procedural programming language  Uses detailed instructions  Processes statements sequentially  Combines SQL commands.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
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.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
XML and DTD Please you speaker notes for additional information!
Examples dealing with cursors and tables Please use speaker notes for additional information!
SQL and Conditions Speaker notes will provide additional information!
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.
Manipulating data within PL/SQL Please use speaker notes for additional information!
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.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
Exceptions in PL/SQL Please use speaker notes for additional information!
Using SQL in PL/SQL ITEC 224 Database Programming.
Introduction to Oracle - SQL Additional information is available in speaker notes!
JavaScript, Fourth Edition
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
implicit and an explicit cursor
Introduction to PL/SQL As usual, use speaker notes for additional information!
Using a Database Access97 Please use speaker notes for additional information!
PHP with MYSQL Please use speaker notes for additional information!
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
A Guide to SQL, Seventh Edition
Introduction to Triggers
Introduction to Procedures
MySQL - Creating donorof database offline
Introduction to Functions
Please use speaker notes for additional information!
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Chapter 8 Advanced SQL.
More and Still More on Procedures and Functions
Handling Data in PL/SQL Blocks
Presentation transcript:

PL/SQL User Defined Types Record and Table Please use speaker notes for additional information!

Records A record is defined as a composite data structure because it is composed of multiple elements (components). The record does not have its own value because it is viewed as a structure to store and access the individual components each of which has its own value. In Oracle there are three types of RECORDS: table-based, cursor-based and programmer-defined. While their uses vary, they have the same internal structure. Every record is composed of one or more elements/fields.

Implicit cursor Implicit cursors are used by PL/SQL blocks whenever an SQL statement is executed if there is not an existing explicit cursor associated with the statement. The implicit cursor is automatically setup by Oracle and is not under the control of the programmer/developer. Oracle sets up what is know as the context area which contains the information Oracle needs to process a statement. The cursor serves as a pointer or handle to the context area. In PL/SQL the implicit cursor is used to process the DML statements such as INSERT, DELETE, UPDATE as well as the SELECT INTO statement.

SET VERIFY OFF ACCEPT in_idno PROMPT 'Please enter the donor idno: ' DECLARE TYPE rec_donor IS RECORD (rec_id VARCHAR2(5), rec_name VARCHAR2(15), rec_yrgoal NUMBER(7,2)); a_rec_donor rec_donor; -- Note: this declares a variable of the defined type BEGIN SELECT idno, name, yrgoal INTO a_rec_donor.rec_id, a_rec_donor.rec_name, a_rec_donor.rec_yrgoal FROM new_donor WHERE idno = &in_idno; INSERT INTO some_donor VALUES(a_rec_donor.rec_id, a_rec_donor.rec_name, a_rec_donor.rec_yrgoal); END; / SET VERIFY ON TYPE - RECORD SQL> edit atsd1 atsd1 Please enter the donor idno: PL/SQL procedure successfully completed. SQL> SELECT * FROM some_donor; IDNO NAME YRGOAL Susan Ash 100 SQL> SELECT * FROM donor 2 WHERE idno = 23456; IDNO NAME STADR CITY ST ZIP DATEFST YRGOAL CONTACT Susan Ash 21 Main St Fall River MA MAR Amy Costa

SET VERIFY OFF ACCEPT in_idno PROMPT 'Please enter the donor idno: ' DECLARE TYPE rec_donor IS RECORD (rec_id VARCHAR2(5), rec_name VARCHAR2(15), rec_yrgoal NUMBER(7,2)); a_rec_donor rec_donor; -- Note: this declares a variable of the defined type BEGIN SELECT idno, name, yrgoal INTO a_rec_donor FROM new_donor WHERE idno = &in_idno; INSERT INTO some_donor VALUES(a_rec_donor.rec_id, a_rec_donor.rec_name, a_rec_donor.rec_yrgoal); END; / SET VERIFY ON TYPE - RECORD SQL> edit atsd2 SQL> SELECT * FROM donor WHERE idno = 11111; IDNO NAME STADR CITY ST ZIP DATEFST YRGOAL CONTACT Stephen Daniels 123 Elm St Seekonk MA JUL John Smith Please enter the donor idno: PL/SQL procedure successfully completed. SQL> SELECT * FROM some_donor; IDNO NAME YRGOAL Susan Ash Stephen Daniels 500

SET VERIFY OFF ACCEPT in_idno PROMPT 'Please enter the donor idno: ' DECLARE TYPE rec_donor IS RECORD (rec_id VARCHAR2(5), rec_name VARCHAR2(15), rec_yrgoal NUMBER(7,2)); a_rec_donor rec_donor; -- Note: this declares a variable of the defined type BEGIN /*This version does not work because of a_rec_donor in the VALUES clause */ SELECT idno, name, yrgoal INTO a_rec_donor FROM new_donor WHERE idno = &in_idno; INSERT INTO some_donor VALUES(a_rec_donor); END; / SET VERIFY ON TYPE - RECORD SQL> edit atsd2 atsd2a Please enter the donor idno: VALUES(a_rec_donor); * ERROR at line 10: ORA-06550: line 10, column 11: PLS-00382: expression is of wrong type ORA-06550: line 9, column 4: PL/SQL: SQL Statement ignored

ROWTYPE SET VERIFY OFF ACCEPT in_idno PROMPT 'Please enter the donor idno: ' DECLARE v_donor some_donor%ROWTYPE; BEGIN SELECT idno, name, yrgoal INTO v_donor FROM new_donor WHERE idno = &in_idno; INSERT INTO some_donor VALUES(v_donor.idno, v_donor.name, v_donor.yrgoal); END; / SET VERIFY ON SQL> edit atsd4 SQL> DESC some_donor Name Null? Type IDNO VARCHAR2(5) NAME VARCHAR2(15) YRGOAL NUMBER(7,2)

ROWTYPE SQL> SELECT * FROM some_donor; IDNO NAME YRGOAL Susan Ash Stephen Daniels 500 atsd4 Please enter the donor idno: PL/SQL procedure successfully completed. SQL> SELECT * FROM some_donor; IDNO NAME YRGOAL Susan Ash Stephen Daniels Carl Hersey

TABLES PL/SQL tables are the answer to the need for an array structure. An array is basically a temporary table in memory available during the session. They are not database tables! The PL/SQL table is indexed by a binary integer. To declare a PL/SQL table: first define the table structure using TYPE … IS TABLE and then once the type has been created, you can declare the actual table.

SET SERVEROUTPUT ON DECLARE TYPE t_table1 IS TABLE OF VARCHAR2(12) INDEX BY BINARY_INTEGER; v_table_var t_table1; v_count_result NUMBER; BEGIN v_table_var(5) := 'Fifth week'; v_table_var(2) := 'Second week'; v_table_var(12) := 'Twelfth week'; v_table_var(6) := 'Sixth week'; v_count_result:= v_table_var.COUNT; dbms_output.put_line('The count is: '||v_count_result); END; / SET SERVEROUTPUT OFF SQL> edit table1 TYPE..TABLE table1 The count is: 4 PL/SQL procedure successfully completed.

SET SERVEROUTPUT ON DECLARE TYPE t_table1 IS TABLE OF VARCHAR2(12) INDEX BY BINARY_INTEGER; v_table_var t_table1; v_count_result NUMBER; v_place NUMBER; BEGIN v_table_var(5) := 'Fifth week'; v_table_var(2) := 'Second week'; v_table_var(12) := 'Twelfth week'; v_table_var(6) := 'Sixth week'; v_place := v_table_var.FIRST; FOR x IN 1..4 LOOP dbms_output.put_line('The current place is: ' || v_place); v_place := v_table_var.NEXT(v_place); END LOOP; v_count_result:= v_table_var.COUNT; dbms_output.put_line('The count is: '||v_count_result); END; / SET SERVEROUTPUT OFF SQL> edit table1a TYPE..TABLE table1a The current place is: 2 The current place is: 5 The current place is: 6 The current place is: 12 The count is: 4 Output:

SET SERVEROUTPUT ON DECLARE TYPE t_table1 IS TABLE OF VARCHAR2(12) INDEX BY BINARY_INTEGER; v_table_var t_table1; v_count_result NUMBER; v_num NUMBER :=&in_num; BEGIN v_table_var(5) := 'Fifth week'; v_table_var(2) := 'Second week'; v_table_var(12) := 'Twelfth week'; v_table_var(6) := 'Sixth week'; v_count_result:= v_table_var.COUNT; dbms_output.put_line('The count is: '||v_count_result); IF v_table_var.EXISTS(v_num) THEN dbms_output.put_line('The record exists'); ELSE dbms_output.put_line('The record does not exist'); END IF; END; / SET SERVEROUTPUT OFF SQL> edit table2 TYPE..TABLE Enter value for in_num: 5 old 6: v_num NUMBER :=&in_num; new 6: v_num NUMBER :=5; The count is: 4 The record exists PL/SQL procedure successfully completed. table2 Enter value for in_num: 8 old 6: v_num NUMBER :=&in_num; new 6: v_num NUMBER :=8; The count is: 4 The record does not exist PL/SQL procedure successfully completed.

TABLES SET SERVEROUTPUT ON DECLARE input_area VARCHAR2(20) :='South Shore MA'; TYPE t_areacode_type IS TABLE OF VARCHAR2(20) INDEX BY BINARY_INTEGER; v_area t_areacode_type; v_index NUMBER; BEGIN v_area(508) := 'Southeastern MA'; v_area(617) := 'Boston MA'; v_area(781) := 'South Shore MA'; v_area(401) := 'Rhode Island'; v_area(999) := 'region not found'; v_index := v_area.FIRST; LOOP DBMS_OUTPUT.PUT_LINE(v_area(v_index) ||' ' || v_index); EXIT WHEN v_index = v_area.LAST OR v_area(v_index) = input_area; v_index := v_area.NEXT(v_index); END LOOP; DBMS_OUTPUT.PUT_LINE('Match: ' || v_area(v_index) ||' ' || v_index); END; / SET SERVEROUTPUT OFF SQL> edit areacodett I am starting out with an input area which I assign the value of South Shore MA. My goal is to search the table until I find a match. To do this, I first set up the table and then I assigned values to the table. Finally I looped through the table displaying my progress until a match was found. When a match is found I exit the loop and display the match. Output: areacodett Rhode Island 401 Southeastern MA 508 Boston MA 617 South Shore MA 781 Match: South Shore MA 781

SET SERVEROUTPUT ON DECLARE input_area VARCHAR2(20) :=‘Boston MA'; TYPE t_areacode_type IS TABLE OF VARCHAR2(20) INDEX BY BINARY_INTEGER; v_area t_areacode_type; v_index BINARY_INTEGER; BEGIN v_area(508) := 'Southeastern MA'; v_area(617) := 'Boston MA'; v_area(781) := 'South Shore MA'; v_area(401) := 'Rhode Island'; v_area(999) := 'region not found'; v_index := v_area.FIRST; LOOP DBMS_OUTPUT.PUT_LINE(v_area(v_index) ||' ' || v_index); EXIT WHEN v_index = v_area.LAST OR v_area(v_index) = input_area; v_index := v_area.NEXT(v_index); END LOOP; DBMS_OUTPUT.PUT_LINE('Match: ' || v_area(v_index) ||' ' || v_index); END; / SET SERVEROUTPUT OFF SQL> edit areacodett Output: areacodett Rhode Island 401 Southeastern MA 508 Boston MA 617 Match: Boston MA 617 TYPE..TABLE In this example, processing stops when Boston is encountered. This causes the EXIT because v_area(v_index) = input_area.

SET SERVEROUTPUT ON DECLARE input_area VARCHAR2(20) :=‘XXX MA'; TYPE t_areacode_type IS TABLE OF VARCHAR2(20) INDEX BY BINARY_INTEGER; v_area t_areacode_type; v_index NUMBER; BEGIN v_area(508) := 'Southeastern MA'; v_area(617) := 'Boston MA'; v_area(781) := 'South Shore MA'; v_area(401) := 'Rhode Island'; v_area(999) := 'region not found'; v_index := v_area.FIRST; LOOP DBMS_OUTPUT.PUT_LINE(v_area(v_index) ||' ' || v_index); EXIT WHEN v_index = v_area.LAST OR v_area(v_index) = input_area; v_index := v_area.NEXT(v_index); END LOOP; DBMS_OUTPUT.PUT_LINE('Match: ' || v_area(v_index) ||' ' || v_index); END; / SET SERVEROUTPUT OFF SQL> edit areacodett Output: areacodett Rhode Island 401 Southeastern MA 508 Boston MA 617 South Shore MA 781 region not found 999 Match: region not found 999 TYPE…TABLE In this example, there is no match for XXX MA, so when all elements of the array have been completed, processing ends. The LAST attribute is what makes this work. When v_index which keeps getting incremented to the NEXT number finall equals the last number in the table, processing ends.