SQL Objects and PL/SQL. Who am I ?  Gary Myers  Oracle developer since 1994  Database Consultant with SMS M&T  Blogger since 2004 Now at blog.sydoracle.com.

Slides:



Advertisements
Similar presentations
PL/SQL : Stop making the same performance mistakes
Advertisements

Oracle PL/SQL IV Exceptions Packages.
Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
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.
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.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
ISD3 Chris Wallace Next 6 Weeks Extended Relational Model Object Orientation Matching systems 3 tier architecture Technology.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
PL/SQL Bulk Collections in Oracle 9i and 10g Kent Crotty Burleson Consulting October 13, 2006.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
ORACLE ONLINE TRAINING Contact our Support Team : SOFTNSOL India: Skype id : softnsoltrainings id:
SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Introduction to DBMS and SQL Introduction to DBMS and SQL GUIDED BY : MR. YOGESH SAROJ (PGT-CS) MR. YOGESH SAROJ (PGT-CS) Presented By : JAYA XII –COM.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
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.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 11 Introduction to Dynamic SQL and Object Technology.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
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.
Stored Procedures, Transactions, and Error-Handling
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Records Oracle Database PL/SQL 10g Programming Chapter 5.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
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.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
Chapter 6 Procedural Language SQL and Advanced SQL Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Collections Oracle Database PL/SQL 10g Programming Chapter 6.
Database Application Development using PL/SQL Programming.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Stored Procedures. Definition a stored procedure is a set of Structured Query Language (SQL) statements with an assigned name that's stored in the database.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
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.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
Query Processing – Implementing Set Operations and Joins Chap. 19.
Oracle & SQL. Oracle Data Types Character Data Types: Char(2) Varchar (20) Clob: large character string as long as 4GB Bolb and bfile: large amount of.
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.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
There’s a particular style to it… Rob Hatton
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
 CONACT UC:  Magnific training   
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
More SQL: Complex Queries,
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
ORACLE SQL Developer & SQLPLUS Statements
Using the Set Operators
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Chapter 10 Oracle9i Developer: PL/SQL Programming
Database systems Lecture 3 – SQL + CRUD
SQL Fundamentals in Three Hours
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
PL/SQL Declaring Variables.
Presentation transcript:

SQL Objects and PL/SQL

Who am I ?  Gary Myers  Oracle developer since 1994  Database Consultant with SMS M&T  Blogger since 2004 Now at blog.sydoracle.com  Twitter at syd_oracle  Guest editor at Log Buffer  "Oracle" badge at Stackoverflow forums  Find me at LinkedIn…

What is PL/SQL  PL/SQL is an EXTENSION to SQL  PL/SQL is procedural  Closely tied to the database  Runs under the oracle shadow process  Mostly in the PGA BEWARE Shared Server (eg APEX)

PL/SQL Datatypes  As an extension, PL/SQL recognizes all SQL datatypes (except LONG)  That includes user-defined datatypes  User-defined datatypes includes Oracle supplied ones such as Spatial (MDSYS) and Multimedia (ORDSYS).

PL/SQL specific datatypes  Boolean, PLS_INTEGER etc  Only within PL/SQL  Cannot be used to ‘talk’ to SQL. Avoid as function parameters  Some potential for better performance and/or memory consumption

PL/SQL Record Types CREATE PROCEDURE TEST TYPE typ_rec is RECORD (id NUMBER, val VARCHAR2(20)); t_rec1 typ_rec; t_rec2 typ_rec; BEGIN t_rec1.id := 5; t_rec1.val := 'TEST'; t_rec2 := t_rec1; t_rec1 := null; END;

SQL Object Type CREATE TYPE typ_rec is OBJECT (id NUMBER, val VARCHAR2(20)); / SELECT typ_rec(1,’test’) FROM DUAL;

Constructor Methods CREATE TYPE holt AS OBJECT (record_type varchar2(1), record_column varchar2(10), CONSTRUCTOR FUNCTION holt ( i_record_type in varchar2 default null, i_record_column in varchar2 default null) return self as result);

Constructor Method Body CREATE OR REPLACE TYPE body holt is CONSTRUCTOR FUNCTION holt (i_record_type in varchar2 default null, i_record_column in varchar2 default null) return self as result is begin self.record_type := i_record_type; self.record_column := i_record_column; RETURN; end holt; end;

PL/SQL using SQL Type declare v_holt holt; begin v_holt := holt( i_record_type => 'a', i_record_column => 'B'); end; /

PL/SQL Collection Types CREATE PROCEDURE TEST TYPE typ_coll_vc10 is TABLE OF VARCHAR2(10) INDEX BY PLS_INTEGER; tc_val typ_coll_vc10; BEGIN tc_val(1) := 'TEST'; END;

SQL Collection Types  SQL Collection types cannot have member functions.  They do have functionality equivalent to a default constructor though  You can create an object that includes a collection

SQL collection as Table  You can select from a collection using the syntax SELECT column_value from TABLE( tab_varchar_4000( 'a','b','c'));

FREE There's a default collection type in the database, granted to public SYS.DBMS_DEBUG_VC2COLL select * from table(sys.dbms_debug_vc2coll ('a','b','c'))

Functions returning collections CREATE FUNCTION gen_dates (i_start in date, i_end in date) RETURN tab_varchar_4000 IS v_tab tab_varchar_4000 := tab_varchar_4000(); BEGIN v_tab.extend(1 + i_end - i_start ); FOR i in 0..( i_end - i_start ) LOOP v_tab(i+1) := i_start + i; END LOOP; RETURN v_tab; END;

Pipelined functions CREATE FUNCTION gen_dates RETURN tab_varchar_4000 PIPELINED IS CURSOR c_1 is select sysdate dt from dual connect by level < 100; BEGIN FOR i in c_1 LOOP PIPE ROW (to_char(i.dt)); END LOOP; RETURN; END;

Wouldn't it be nice…. procedure p_test is Begin dbms_output.put_line('point 1');... dbms_output.put_line('point 2');... End;

Does not allow DML Function p_test return tab_varchar_4000 pipelined is Begin pipe row('point 1'); update…. pipe row('point 2'); End;

Must be at the top level Procedure process_records is procedure debug (p_text in varchar2) is begin pipe row (p_text); end; Begin... End;

Warning – No Data Found TABLE() functions complete with a NO_DATA_FOUND

It gets worse Non-existent collection elements also return a no_data_found

Pipelined Functions and PL/SQL Collections A PL/SQL collection type can also be used in a pipelined function It implicitly creates three SQL level-types (starting with SYS_PLSQL_object_id) These types can be amended with ALTER TYPE (but that's not a good idea)

INDEX BY VARCHAR2 Added in 9iR2 Allows a degree of caching in PL/SQL Useful if you have an expensive calculation that you can reuse (much like the function result cache in 11g).

SQL collection operators COLLECT will aggregate up a set of data items into an on-the-fly collection select collect('test') from dual connect by level < 10; You can CAST that collection to your own collection type, if compatible. Beware of CHAR / VARCHAR2 incompatibilities

Like any Aggregate Function DISTINCT select collect(distinct to_char(sysdate)) from dual connect by level < 5; GROUP BY select cast(collect(object_name) as tab_varchar_4000) t from user_objects group by object_type;

Collections and Sets Definition : A collection is a set if it doesn't have duplicates

SET testing select 1 a from dual where mset('a','b','a') is a set; no rows selected select 1 a from dual where mset('a','b') is a set; A

MULTISET operators Turn a collection into a set. select mset('z','a','a','c','d','c') multiset union distinct mset() from dual; There are also INTERSECT and EXCEPT (not MINUS)

SET comparison select 1 from dual where 'a' member mset('a','b'); 1 select 1 from dual where mset('a') submultiset mset('a','b'); 1 select 1 a from dual where mset('a','c') submultiset mset('a','b'); no rows selected

Collection comparison You can compare simple collections in PL/SQL with an equals operator declare x1 mset := mset('a','b'); x2 mset := mset('b','a'); begin if x1 = x2 then dbms_output.put_line('Match'); end if; end;

Multiset operators in PL/SQL declare x1 mset := mset('a','b'); x2 mset := mset('b'); x3 mset; begin x3 := x1 multiset except x2; dbms_output.put_line(x3.count || '.'||x3(1)); end;

Final Thought For complex logic SQL*Plus embedded in shell scripts is NOT a good idea