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.

Slides:



Advertisements
Similar presentations
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
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.
Advanced Package Concepts. 2 home back first prev next last What Will I Learn? Write packages that use the overloading feature Write packages that use.
Creating Packages. 2 home back first prev next last What Will I Learn? Describe the reasons for using a package Describe the two components of a package:
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.
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions.
Introduction to PL/SQL Chapter 9. Objectives Explain the need for PL/SQL Explain the benefits of PL/SQL Identify the different types of PL/SQL blocks.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Benefits of PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –List and explain the benefits of PL/SQL –List.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 11 Introduction to Dynamic SQL and Object Technology.
Copyright  Oracle Corporation, All rights reserved. 3 Creating Procedures.
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.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
Copyright Ó Oracle Corporation, All rights reserved. 22 Running a Form Builder Application.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 6 Functions.
PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions.
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.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
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.
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.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
12 Copyright © 2004, Oracle. All rights reserved. Understanding and Influencing the PL/SQL Compiler.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Dynamic SQL Oracle Database PL/SQL 10g Programming Chapter 13.
Handling Exceptions. 2 home back first prev next last What Will I Learn? Describe several advantages of including exception handling code in PL/SQL Describe.
Creating DDL and Database Event Triggers. 2 home back first prev next last What Will I Learn? Describe events that cause DDL and database event triggers.
Using Oracle-Supplied Packages. 2 home back first prev next last What Will I Learn? Describe two common uses for the DBMS_OUTPUT server-supplied package.
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.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
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.
Using SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4.
Using Functions in SQL Statements. 2 home back first prev next last What Will I Learn? List the advantages of user-defined functions in SQL statements.
Passing Parameters. 2 home back first prev next last What Will I Learn? List the types of parameter modes Create a procedure that passes parameters Identify.
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Stored Procedures / Session 4/ 1 of 41 Session 4 Module 7: Introducing stored procedures Module 8: More about stored procedures.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
SQL Triggers, Functions & Stored Procedures Programming Operations.
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.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
Creating Stored Functions
A Guide to SQL, Seventh Edition
Oracle11g: PL/SQL Programming Chapter 5 Procedures.
PL/SQL MULTIPLE CHOICE QUESTION.
Interacting with the Oracle Server
UNIT - V STORED PROCEDURE.
REF Cursors.
PL/SQL Scripting in Oracle:
PRACTICE OVERVIEW PL/SQL Part - 2.
Database Management Systems 2
Chapter 10 Oracle9i Developer: PL/SQL Programming
Creating Noninput Items
PRACTICE OVERVIEW PL/SQL Part - 1.
Chapter 8 Advanced SQL.
Prof. Arfaoui. COM390 Chapter 6
Handling Data in PL/SQL Blocks
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

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 dynamic SQL to create a SQL statement List four PL/SQL statements supporting Native Dynamic SQL Describe the benefits of EXECUTE IMMEDIATE over DBMS_SQL for Dynamic SQL

3 home back first prev next last Why Learn It? In this lesson, you learn to construct and execute SQL statements dynamically—in other words, at run time using the Native Dynamic SQL statements in PL/SQL. Dynamically executing SQL and PL/SQL code extends the capabilities of PL/SQL beyond query and transactional operations. The lesson also compares Native Dynamic SQL to the DBMS_SQL package, which provides similar capabilities.

4 home back first prev next last Execution Flow of SQL All SQL statements in the database go through various stages: –Parse: pre-execution “is this possible?” checks including syntax, object existence, privileges and so on –Bind: getting the actual values of any variables referenced in the statement –Execute: the statement is executed –Fetch: results are returned to the user. Some stages may not be relevant for all statements—for example, the fetch phase is applicable to queries but not DML.

5 home back first prev next last Execution Flow of SQL in PL/SQL Subprograms When a SQL statement is included in a PL/SQL subprogram, the parse and bind phases are normally done at compile time, ie when the procedure, function or package body is CREATEd. What if the text of the SQL statement is not known when the procedure is created ? How could the Oracle server parse it ? –It couldn’t. –For example, suppose we want to DROP a table, but the user will enter the table name at execution time:

6 home back first prev next last Dynamic SQL We use dynamic SQL to create a SQL statement whose text is not completely known in advance. Dynamic SQL: – Is constructed and stored as a character string within a subprogram. – Is a SQL statement with varying column data, or different conditions with or without placeholders (bind variables) – Enables data-definition, data-control, or session- control statements to be written and executed from PL/SQL – Is executed with Native Dynamic SQL statements (EXECUTE IMMEDIATE) or the DBMS_SQL package Dynamic SQL statements are parsed at execution time, not at compile time.

7 home back first prev next last Native Dynamic SQL Provides native support for Dynamic SQL directly in the PL/SQL language Provides the ability to execute SQL statements whose structure is unknown until execution time Uses the EXECUTE IMMEDIATE PL/SQL statement Can also use the OPEN-FOR, FETCH and CLOSE PL/SQL statements.

8 home back first prev next last Using the EXECUTE IMMEDIATE Statement Use the EXECUTE IMMEDIATE statement for Native Dynamic SQL in PL/SQL anonymous blocks or subprograms: INTO is used for single-row queries and specifies the variables or records into which column values are retrieved. USING is used to hold all bind arguments. The default parameter mode is IN, if not specified.

9 home back first prev next last Using the EXECUTE IMMEDIATE Statement  dynamic_string is a character variable or literal containing the text of a SQL statement.  define_variable is a PL/SQL variable that stores a selected column value.  record is a user-defined or %ROWTYPE record that stores a selected row.  bind_argument is an expression whose value is passed to the dynamic SQL statement at execution time.  USING clause holds all bind arguments. The default parameter mode is IN.

10 home back first prev next last Example 1: Dynamic SQL with a DDL Statement Constructing the dynamic statement in-line: Constructing the dynamic statement in a variable:

11 home back first prev next last Example 2: Dynamic SQL with a DML Statement Deleting all the rows from any table and returning a count: Invoking the function:

12 home back first prev next last Example 3: Dynamic SQL with a DML Statement Inserting a row into a table with two columns: Invoking the procedure:

13 home back first prev next last Example 3: Dynamic SQL with a DML Statement CREATE OR REPLACE PROCEDURE add_row(p_table_name VARCHAR2, p_id NUMBER, p_name VARCHAR2) IS v_sql VARCHAR2(200); BEGIN v_sql := 'INSERT INTO ' || p_table_name || ' VALUES (' || p_id || ', ''' || p_name ||''')'; dbms_output.put_line(v_sql); EXECUTE IMMEDIATE v_sql; COMMIT; END; /

14 home back first prev next last Example 3: Dynamic SQL with a DML Statement -- 另一种解决方案 CREATE OR REPLACE PROCEDURE add_row(p_table_name VARCHAR2, p_id NUMBER, p_name VARCHAR2) IS v_sql VARCHAR2(200); BEGIN v_sql := 'INSERT INTO ' || p_table_name || ' VALUES (:v1, :v2)'; dbms_output.put_line(v_sql); EXECUTE IMMEDIATE v_sql USING p_id, p_name; COMMIT; END; /

15 home back first prev next last Example 4: Using Native Dynamic SQL to Recompile PL/SQL Code PL/SQL objects can be recompiled without recreating them by using the following ALTER statements: The next slide shows a procedure which recompiles a PL/SQL object whose name and type is entered at run-time.

16 home back first prev next last Example 4 continued Creating the procedure: Example invocation:

17 home back first prev next last Using the DBMS_SQL Package Prior to Oracle 8i, the EXECUTE IMMEDIATE statement did not exist in PL/SQL, and the presupplied DBMS_SQL package was the only way to write dynamic SQL. Some of the procedures and functions of the DBMS_SQL package are: –OPEN_CURSOR –PARSE –BIND_VARIABLE –EXECUTE –FETCH_ROWS –CLOSE_CURSOR

18 home back first prev next last Using DBMS_SQL with a DML Statement Example of deleting rows: Compare this with the del_rows function earlier in this lesson. They are functionally identical, but which is simpler?

19 home back first prev next last Using DBMS_SQL with a Parameterized DML Statement Again, compare this with the add_row procedure earlier in this lesson. Which would you rather write? Same Error as before.

20 home back first prev next last Comparison of Native Dynamic SQL and the DBMS_SQL Package Native Dynamic SQL: –Is easier to use than DBMS_SQL –Requires less code than DBMS_SQL –Often executes faster than DBMS_SQL because there are fewer statements to execute.

21 home back first prev next last Terminology Key terms used in this lesson include: –Dynamic SQL –EXECUTE IMMEDIATE

22 home back first prev next last Summary In this lesson, you learned to: –Recall the stages through which all SQL statements pass –Describe the reasons for using dynamic SQL to create a SQL statement –List four PL/SQL statements supporting Native Dynamic SQL –Describe the benefits of EXECUTE IMMEDIATE over DBMS_SQL for Dynamic SQL

23 home back first prev next last Try It / Solve It The exercises in this lesson cover the following topics: –Recalling the stages through which all SQL statements pass –Describing the reasons for using dynamic SQL to create a SQL statement –Listing four PL/SQL statements supporting Native Dynamic SQL –Describing the benefits of EXECUTE IMMEDIATE over DBMS_SQL for Dynamic SQL