Dynamic SQL Oracle Database PL/SQL 10g Programming Chapter 13.

Slides:



Advertisements
Similar presentations
Basic SQL Introduction Presented by: Madhuri Bhogadi.
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.
April 14, 2004 Next Information Systems 1 Bulk Collections and Inserts in Oracle 9i & 10g Simay Alpöge Next Information Systems, Inc.
Chapter 4B: More Advanced PL/SQL Programming
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
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.
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.
Exceptions Oracle Database PL/SQL 10g Programming Chapter 7.
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.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Oracle PL/SQL Practices. Critical elements of PL/SQL Best Practices Build your development toolbox Unit test PL/SQL programs Optimize SQL in PL/SQL programs.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Records Oracle Database PL/SQL 10g Programming Chapter 5.
Program with PL/SQL. Interacting with the Oracle Server.
Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
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.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Database Technology Jing Shen.
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Intersession Communication Oracle Database PL/SQL 10g Programming
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
ITEC 224 Database Programming PL/SQL Lab Cursors.
Variables and control statements in PL\SQL Chapter 10.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
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.
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.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
implicit and an explicit cursor
Using SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Oracle 10g Database Administrator: Implementation and Administration Chapter 10 Basic Data Management.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
Last Updated : 27 th April 2004 Center of Excellence Data Warehousing Group Teradata RDBMS Concepts.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
1 Chapter 5: Advanced PL/SQL Programming. 2 Anonymous PL/SQL Programs Write code in text editor, execute it in SQL*Plus Code can be stored as text in.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
Chapter 2 Anonymous Block
A Guide to SQL, Seventh Edition
PL/SQL MULTIPLE CHOICE QUESTION.
Advanced Accounting Information Systems
Interacting with the Oracle Server
REF Cursors.
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
variables and control statements in PL\SQL
Chapter 10 Oracle9i Developer: PL/SQL Programming
SQL Queries Chapter No 3.
HAVING,INDEX,COMMIT & ROLLBACK
Chapter 8 Advanced SQL.
Handling Data in PL/SQL Blocks
Presentation transcript:

Dynamic SQL Oracle Database PL/SQL 10g Programming Chapter 13

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 2 Dyanmic SQL Defining Dynamic SQL Defining Dynamic SQL Working with Native Dynamic SQL Working with Native Dynamic SQL Working with DBMS_SQL Built-in Working with DBMS_SQL Built-in

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 3 Dynamic SQL Defining Dynamic SQL Build and run SQL statements on the fly. Build and run SQL statements on the fly. Evaluates dependencies at runtime. Evaluates dependencies at runtime. Defines column and table names at runtime. Defines column and table names at runtime. Lets you define polymorphic statements. Lets you define polymorphic statements. Build dynamic SQL statements by using: Build dynamic SQL statements by using: Native Dynamic SQL (NDS) uses EXECUTE IMMEDIATE to run dynamic statements. Native Dynamic SQL (NDS) uses EXECUTE IMMEDIATE to run dynamic statements. DBMS_SQL Built-in provides a collection of overloaded functions and procedures to build and execute dynamic queries. DBMS_SQL Built-in provides a collection of overloaded functions and procedures to build and execute dynamic queries.

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 4 Dynamic SQL Native Dynamic SQL (NDS) Enables dynamic DDL and DML SQL statements without bind variables. Enables dynamic DDL and DML SQL statements without bind variables. Enables dynamic DML with a known list of bind variables. Enables dynamic DML with a known list of bind variables. Enables dynamic DQL. Enables dynamic DQL.

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 5 Dynamic SQL NDS Advantages It performs faster than DBMS_SQL. It performs faster than DBMS_SQL. It has syntax that mirrors standard SQL syntax. It has syntax that mirrors standard SQL syntax. It fetches directly into PL/SQL record types. It fetches directly into PL/SQL record types. It supports all PL/SQL data types, including user-defined data types. It supports all PL/SQL data types, including user-defined data types.

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 6 Dynamic SQL NDS Advantages It supports bulk processing: It supports bulk processing: BULK FETCH statement BULK FETCH statement BULK EXECUTE IMMEDIATE statement BULK EXECUTE IMMEDIATE statement FORALL statement FORALL statement COLLECT INTO clause COLLECT INTO clause RETURNING INTO clause RETURNING INTO clause %BULK_ROWCOUNT attribute %BULK_ROWCOUNT attribute

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 7 Dynamic SQL NDS: EXECUTE IMMEDIATE Clauses INTO INTO Mode is OUT only. Mode is OUT only. Supports only single row return statements. Supports only single row return statements. RETURNING INTO RETURNING INTO Modes are IN and OUT. Modes are IN and OUT. Supports name positional notation assigning variables to bind variables. Supports name positional notation assigning variables to bind variables. USING USING Modes are IN and OUT by itself. Modes are IN and OUT by itself. Mode is IN only when concurrently using RETURNING INTO clause. Mode is IN only when concurrently using RETURNING INTO clause. Supports only positional notation. Supports only positional notation.

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 8 Dynamic SQL NDS: Without bind variables DECLARE retval NUMBER; retval NUMBER; statement VARCHAR2(4000); statement VARCHAR2(4000);BEGIN statement := 'CREATE SEQUENCE ' || a_sequence || 'INCREMENT BY 1'; statement := 'CREATE SEQUENCE ' || a_sequence || 'INCREMENT BY 1'; EXECUTE IMMEDIATE statement; EXECUTE IMMEDIATE statement;END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 9 Dynamic SQL NDS: Without substitution variables DECLARE retval NUMBER; retval NUMBER; sequence_name VARCHAR2(30) := 'sequence_name'; sequence_name VARCHAR2(30) := 'sequence_name'; statement VARCHAR2(4000); statement VARCHAR2(4000);BEGIN statement := 'CREATE SEQUENCE '||sequence_name||' ' statement := 'CREATE SEQUENCE '||sequence_name||' ' || 'INCREMENT BY 1'; || 'INCREMENT BY 1'; EXECUTE IMMEDIATE statement; EXECUTE IMMEDIATE statement;END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 10 Dynamic SQL NDS: With bind variables DECLARE retval NUMBER; retval NUMBER; sequence_name VARCHAR2(30) := 'sequence_name'; sequence_name VARCHAR2(30) := 'sequence_name'; statement VARCHAR2(4000); statement VARCHAR2(4000);BEGIN statement := 'CREATE SEQUENCE :sequence_name ' statement := 'CREATE SEQUENCE :sequence_name ' || 'INCREMENT BY 1'; || 'INCREMENT BY 1'; EXECUTE IMMEDIATE statement USING sequence_name; EXECUTE IMMEDIATE statement USING sequence_name;END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 11 Dynamic SQL NDS: With bind output variables DECLARE column_table1 VARCHAR2_NESTED_TABLE; column_table1 VARCHAR2_NESTED_TABLE; column_table2 VARCHAR2_NESTED_TABLE; column_table2 VARCHAR2_NESTED_TABLE;BEGIN statement := 'BEGIN ' statement := 'BEGIN ' || 'SELECT column_name1, column_name2 ' || 'SELECT column_name1, column_name2 ' || 'BULK COLLECT INTO :column_table ' || 'BULK COLLECT INTO :column_table ' || 'FROM table_name;' || 'FROM table_name;' || 'END;'; || 'END;'; EXECUTE IMMEDIATE statement EXECUTE IMMEDIATE statement USING OUT column_table1, OUT column_table2; USING OUT column_table1, OUT column_table2;END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 12 Dynamic SQL DBMS_SQL Built-in Enables dynamic DDL and DML SQL statements without bind variables. Enables dynamic DDL and DML SQL statements without bind variables. Enables dynamic DML with a known list of bind variables. Enables dynamic DML with a known list of bind variables. Enables dynamic DQL. Enables dynamic DQL.

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 13 Dynamic SQL DBMS_SQL Built-in: Without bind variables DECLARE c INTEGER := DBMS_SQL.OPEN_CURSOR; c INTEGER := DBMS_SQL.OPEN_CURSOR; fdbk INTEGER; fdbk INTEGER; statement VARCHAR2(4000); statement VARCHAR2(4000);BEGIN statement := 'CREATE SEQUENCE ' || a_sequence statement := 'CREATE SEQUENCE ' || a_sequence || 'INCREMENT BY 1'; || 'INCREMENT BY 1'; DBMS_SQL.PARSE(c,statement,DBMS_SQL.NATIVE); DBMS_SQL.PARSE(c,statement,DBMS_SQL.NATIVE); fdbk := DBMS_SQL.EXECUTE(c); fdbk := DBMS_SQL.EXECUTE(c); DBMS_SQL.CLOSE_CURSOR(c); DBMS_SQL.CLOSE_CURSOR(c);END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 14 Dynamic SQL DBMS_SQL Built-in: With bind variables DECLARE c INTEGER := DBMS_SQL.OPEN_CURSOR; c INTEGER := DBMS_SQL.OPEN_CURSOR; fdbk INTEGER; fdbk INTEGER; input VARCHAR2(30); input VARCHAR2(30); statement VARCHAR2(4000); statement VARCHAR2(4000);BEGIN statement := 'INSERT INTO a_table ' statement := 'INSERT INTO a_table ' || 'VALUES (:bind_variable)'; || 'VALUES (:bind_variable)'; DBMS_SQL.PARSE(c,statement,DBMS_SQL.NATIVE); DBMS_SQL.PARSE(c,statement,DBMS_SQL.NATIVE); DBMS_SQL.BIND_VALUE(c,bind_variable,input); DBMS_SQL.BIND_VALUE(c,bind_variable,input); fdbk := DBMS_SQL.EXECUTE(c); fdbk := DBMS_SQL.EXECUTE(c); DBMS_SQL.CLOSE_CURSOR(c); DBMS_SQL.CLOSE_CURSOR(c);END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 15 Dynamic SQL DBMS_SQL Built-in: With bind array variables DECLARE c INTEGER := DBMS_SQL.OPEN_CURSOR; c INTEGER := DBMS_SQL.OPEN_CURSOR; fdbk INTEGER; fdbk INTEGER; input VARCHAR2_NESTED_TABLE; input VARCHAR2_NESTED_TABLE; statement VARCHAR2(4000); statement VARCHAR2(4000);BEGIN statement := 'INSERT INTO a_table ' statement := 'INSERT INTO a_table ' || 'VALUES (:bind_variable)'; || 'VALUES (:bind_variable)'; DBMS_SQL.PARSE(c,statement,DBMS_SQL.NATIVE); DBMS_SQL.PARSE(c,statement,DBMS_SQL.NATIVE); DBMS_SQL.BIND_ARRAY(c,bind_variable,input); DBMS_SQL.BIND_ARRAY(c,bind_variable,input); fdbk := DBMS_SQL.EXECUTE(c); fdbk := DBMS_SQL.EXECUTE(c); DBMS_SQL.CLOSE_CURSOR(c); DBMS_SQL.CLOSE_CURSOR(c);END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 16 Dynamic SQL DBMS_SQL Built-in: With output bind variables DECLARE c INTEGER := DBMS_SQL.OPEN_CURSOR; c INTEGER := DBMS_SQL.OPEN_CURSOR; fdbk INTEGER; fdbk INTEGER; output VARCHAR2(4000); output VARCHAR2(4000); statement VARCHAR2(4000); statement VARCHAR2(4000);BEGIN statement := 'SELECT column_value FROM a_table'; statement := 'SELECT column_value FROM a_table'; DBMS_SQL.PARSE(c,statement,DBMS_SQL.NATIVE); DBMS_SQL.PARSE(c,statement,DBMS_SQL.NATIVE); DBMS_SQL.DEFINE_COLUMN(c,1,output,4000); DBMS_SQL.DEFINE_COLUMN(c,1,output,4000); … loop_on_next_page … … loop_on_next_page …END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 17 Dynamic SQL DBMS_SQL Built-in: With output bind variables DECLARE … noted_on_prior_page … … noted_on_prior_page …BEGIN LOOP LOOP EXIT WHEN DBMS_SQL.FETCH_ROWS(c) = 0; EXIT WHEN DBMS_SQL.FETCH_ROWS(c) = 0; DBMS_SQL.COLUMN_VALUE(c,1,output); DBMS_SQL.COLUMN_VALUE(c,1,output); DBMS_OUTPUT.PUT_LINE('Print ['||output||']'); DBMS_OUTPUT.PUT_LINE('Print ['||output||']'); END LOOP; END LOOP;END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 13)Page 18 Summary Defining Dynamic SQL Defining Dynamic SQL Working with Native Dynamic SQL Working with Native Dynamic SQL Working with DBMS_SQL Built-in Working with DBMS_SQL Built-in