PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 11 Introduction to Dynamic SQL and Object Technology.

Slides:



Advertisements
Similar presentations
Chapter 8 Embedded SQL.
Advertisements

CSE 190: Internet E-Commerce Lecture 10: Data Tier.
ISD3 Chris Wallace Next 6 Weeks Extended Relational Model Object Orientation Matching systems 3 tier architecture Technology.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
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.
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.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
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:
I Copyright © 2004, Oracle. All rights reserved. Introduction.
Database Security and Auditing: Protecting Data Integrity and Accessibility Chapter 6 Virtual Private Databases.
1 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Introduction to Object-Relational features of Oracle 9i.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 6 Functions.
PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions.
Advanced SQL: Cursors & Stored Procedures
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Database Technology Jing Shen.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
Dynamic SQL Oracle Database PL/SQL 10g Programming Chapter 13.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
ITEC 224 Database Programming PL/SQL Lab Cursors.
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
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.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 8 Program Unit Dependencies.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 10 Oracle-Supplied Packages, Dynamic SQL, and Hiding Source Code.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
implicit and an explicit cursor
Using SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4.
Chapter 6 Virtual Private Databases
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Oracle9i Developer: PL/SQL Programming Chapter 11 Performance Tuning.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
A Guide to SQL, Seventh Edition
Oracle11g: PL/SQL Programming Chapter 5 Procedures.
Chapter 8 Dependencies, Privileges and Compilation Oracle11g:
Interacting with the Oracle Server
Chapter 10 Oracle11g: PL/SQL Programming Oracle-Supplied Packages,
Interacting with the Oracle Server
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Oracle9i Developer: PL/SQL Programming Chapter 3 PL/SQL Processing.
All Powder Board and Ski
Chapter 7 Program Unit Dependencies Oracle9i Developer:
Database Management Systems 2
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Chapter 10 Oracle9i Developer: PL/SQL Programming
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Prof. Arfaoui. COM390 Chapter 9
Procedures Oracle & MySQL
Prof. Arfaoui. COM390 Chapter 6
Handling Data in PL/SQL Blocks
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 11 Introduction to Dynamic SQL and Object Technology

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming2 Chapter Objectives After completing this lesson, you should be able to understand: –Creating dynamic SQL –Using object technology

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming3 Brewbean’s Challenge Add flexibility to queries, such as allowing shoppers to determine if they want to search product names or descriptions Explore potential capabilities of object technology features for providing greater data consistency and control

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming4 Dynamic SQL Allows identifiers such as column and table names to be provided at run time Two mechanisms available –DBMS_SQL package (Oracle7) –Native dynamic SQL (Oracle8)

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming5 DBMS_SQL – DML Example CREATE OR REPLACE PROCEDURE dyn_dml_sp (p_col VARCHAR2, p_price NUMBER, p_id NUMBER) IS lv_cursor INTEGER; lv_update VARCHAR2(150); lv_rows NUMBER(1); BEGIN --Open Cursor lv_cursor := DBMS_SQL.OPEN_CURSOR;

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming6 DBMS_SQL – DML Example (continued) --Create DML statement lv_update := 'UPDATE bb_product SET ' || p_col || ' = :ph_price WHERE idProduct = :ph_id'; --Parse the statement DBMS_SQL.PARSE(lv_cursor, lv_update, DBMS_SQL.NATIVE); --Associate parameters with placeholders in the statement DBMS_SQL.BIND_VARIABLE(lv_cursor, ':ph_price', p_price); DBMS_SQL.BIND_VARIABLE(lv_cursor, ':ph_id', p_id);

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming7 DBMS_SQL – DML Example (continued) --Run the DML statement lv_rows := DBMS_SQL.EXECUTE(lv_cursor); --Close the cursor DBMS_SQL.CLOSE_CURSOR(lv_cursor); --Save changes COMMIT; --Check how many rows affected DBMS_OUTPUT.PUT_LINE(lv_rows); END;

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming8 DBMS_SQL – DML Example (continued)

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming9 DBMS_SQL – Query Example CREATE OR REPLACE PROCEDURE dyn_query1_sp (p_col IN VARCHAR2, p_value IN VARCHAR2) IS lv_query LONG; lv_status INTEGER; lv_cursor INTEGER; lv_col1 NUMBER(2); lv_col2 VARCHAR2(25); lv_col3 NUMBER(6,2); lv_col4 NUMBER(5,1);

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming10 DBMS_SQL – Query Example (continued) BEGIN --Open the cursor lv_cursor := DBMS_SQL.OPEN_CURSOR; --Build the query lv_query := 'SELECT idProduct, productname, price, stock FROM bb_product WHERE '|| UPPER(p_col) ||' = ' || 'UPPER(:ph_value)'; --Parse the statement DBMS_SQL.PARSE(lv_cursor, lv_query, DBMS_SQL.NATIVE); --Identify data types for each item selected DBMS_SQL.DEFINE_COLUMN(lv_cursor, 1, lv_col1); DBMS_SQL.DEFINE_COLUMN(lv_cursor, 2, lv_col2, 25); DBMS_SQL.DEFINE_COLUMN(lv_cursor, 3, lv_col3); DBMS_SQL.DEFINE_COLUMN(lv_cursor, 4, lv_col4);

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming11 DBMS_SQL – Query Example (continued) --Associate placeholder with a parameter DBMS_SQL.BIND_VARIABLE(lv_cursor, ':ph_value', p_value); --Execute the query lv_status := DBMS_SQL.EXECUTE(lv_cursor); --Fetch row returned and place into PL/SQL variables IF (DBMS_SQL.FETCH_ROWS(lv_cursor) > 0) THEN DBMS_SQL.COLUMN_VALUE(lv_cursor, 1, lv_col1); DBMS_SQL.COLUMN_VALUE(lv_cursor, 2, lv_col2); DBMS_SQL.COLUMN_VALUE(lv_cursor, 3, lv_col3); DBMS_SQL.COLUMN_VALUE(lv_cursor, 4, lv_col4); DBMS_OUTPUT.PUT_LINE(lv_col1||' '||lv_col2||' '||lv_col3||' '||lv_col4); END IF; --Close cursor DBMS_SQL.CLOSE_CURSOR(lv_cursor); END;

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming12 DBMS_SQL – Query Example (continued)

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming13 Native Dynamic SQL Simpler coding More efficient processing Limited capabilities compared to DBMS_SQL package Two methods –EXECUTE IMMEDIATE –OPEN FOR

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming14 Native Dynamic SQL Example CREATE OR REPLACE PROCEDURE dyn_query3_sp (p_col IN VARCHAR2, p_value IN VARCHAR2) IS lv_query VARCHAR2(200); lv_id bb_product.idProduct%TYPE; lv_name bb_product.productname%TYPE; lv_price bb_product.price%TYPE; lv_stock bb_product.stock%TYPE; BEGIN --use a variable to hold the query construction to -- make it more readable

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming15 Native Dynamic SQL Example (continued) lv_query := 'SELECT idProduct, productname, price, stock FROM bb_product WHERE UPPER(' || p_col || ') = UPPER(:ph_value)'; --Run the dynamic query supplying variables to hold the -- return values in the INTO clause and associate the -- parameter to the placeholder with the USING clause EXECUTE IMMEDIATE lv_query INTO lv_id, lv_name, lv_price, lv_stock USING p_value; DBMS_OUTPUT.PUT_LINE(lv_id||' '||lv_name||' ' ||lv_price||' '||lv_stock); END;

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming16 Native Dynamic SQL Example (continued)

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming17 DBMS_SQL vs. Native Dynamic SQL Use native dynamic SQL when: –The number and types of columns to be used is known –The number and type of bind variables is known –To perform DDL –Executing the statement only once or twice –User-defined types such as object and collections are used (not supported by DBMS_SQL) –Fetching rows of data into PL/SQL records (not supported by DBMS_SQL) –SQL statement is less than 32KB in size

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming18 Object Technology Object Types Object Methods Object Relations Object Views

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming19 Object Types Represent an entity such as an address or a person Defined with attributes and methods Can be used as a data type for a table column Can contain multiple data elements or attributes

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming20 Create an Object Type CREATE OR REPLACE TYPE addr_ot AS OBJECT (street1 VARCHAR2(25), street2 VARCHAR2(25), city VARCHAR2(25), state CHAR(2), zip NUMBER(9) );

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming21 Use Object Type CREATE TABLE bb_order (ord_id NUMBER(4), cust_id NUMBER(4), ord_date DATE, total NUMBER(6,2), bill_addr addr_ot, ship_addr addr_ot );

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming22 Object Type Constructor DECLARE lv_bill addr_ot; lv_ship addr_ot; BEGIN lv_bill := addr_ot('11 Bush Dr',NULL, 'Savannah', 'GA', ); lv_ship := addr_ot('812 Scott Lane','Apt #52','Savannah','GA', ); INSERT INTO bb_order VALUES (102,31,'11-NOV-03’,34.50,lv_bill,lv_ship); END;

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming23 DESCRIBE Command

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming24 Methods Add program units to object types Referred to as members Similar to package creation Add function ALTER TYPE addr_ot ADD MEMBER FUNCTION lbl_print RETURN VARCHAR2;

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming25 Object Type Body

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming26 Object Relations Create customer object type CREATE TYPE cust_ot AS OBJECT (cust_id NUMBER(4), first VARCHAR2(15), last VARCHAR2(20), VARCHAR2(25), phone NUMBER(10) ); Create customer table CREATE TABLE bb_cust OF cust_ot (PRIMARY KEY (cust_id));

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming27 Object Relations (continued) Create orders object type CREATE TYPE ord_ot AS OBJECT (ord_id NUMBER(4), cust_ref REF cust_ot, ord_date DATE, total NUMBER(6,2)); Create orders object type CREATE TABLE bb_ord OF ord_ot (PRIMARY KEY (ord_id));

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming28 Object Relations (continued) Use REF variable to establish relation INSERT INTO bb_cust VALUES )); INSERT INTO bb_ord SELECT ord_ot(1,REF(c),SYSDATE,24.50) FROM bb_cust c WHERE cust_id = 12; COMMIT;

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming29 REF Pointers Does not prevent broken relations like a foreign key constraint

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming30 Object Views Provide a layer on top of traditional database designs to enable object features OBJECT OID identifies unique view row identifier CAST and MULTISET handle subquery mapping to view elements

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming31 Object View Example CREATE TYPE bask_ot AS OBJECT (idBasket NUMBER(5), total NUMBER(7,2)) / CREATE TYPE bask_tab AS TABLE OF bask_ot / CREATE TYPE shop_ot AS OBJECT (idShopper NUMBER(4), last_name VARCHAR2(20), city VARCHAR2(20), idBasket bask_tab) /

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming32 Object View Example (continued) CREATE VIEW shop_vu OF shop_ot WITH OBJECT OID(idShopper) AS SELECT s.idShopper, s.lastname, s.city, CAST(MULTISET(SELECT idBasket, total FROM bb_basket b WHERE b.idShopper = s.idShopper) AS bask_tab) FROM bb_shopper s;

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming33 Summary Dynamic SQL allows identifiers and DDL statements to process within PL/SQL DBMS_SQL package and native dynamic SQL are two mechanisms providing dynamic SQL capabilities Native dynamic SQL is simpler to code and executes more efficiently Object types represent an entity that can contain multiple data attributes and program units

PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming34 Summary (continued) Program units in object types are called methods Relationships between object rows are established using REF variables Object views provide a mechanism to use object technology features with a traditional database design