Download DBMS_SQL

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Dynamic SQL
Oracle Database PL/SQL 10g Programming
Chapter 13
Dyanmic SQL



2006
Defining Dynamic SQL
Working with Native Dynamic SQL
Working with DBMS_SQL Built-in
Oracle Database PL/SQL 10g Programming
(Chapter 13)
Page 2
Dynamic SQL
Defining Dynamic SQL





Build and run SQL statements on the fly.
Evaluates dependencies at runtime.
Defines column and table names at runtime.
Lets you define polymorphic statements.
Build dynamic SQL statements by using:


2006
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.
Oracle Database PL/SQL 10g Programming
(Chapter 13)
Page 3
Dynamic SQL
Native Dynamic SQL (NDS)



2006
Enables dynamic DDL and DML SQL
statements without bind variables.
Enables dynamic DML with a known
list of bind variables.
Enables dynamic DQL.
Oracle Database PL/SQL 10g Programming
(Chapter 13)
Page 4
Dynamic SQL
NDS Advantages




2006
It performs faster than DBMS_SQL.
It has syntax that mirrors standard
SQL syntax.
It fetches directly into PL/SQL record
types.
It supports all PL/SQL data types,
including user-defined data types.
Oracle Database PL/SQL 10g Programming
(Chapter 13)
Page 5
Dynamic SQL
NDS Advantages

It supports bulk processing:






2006
BULK FETCH statement
BULK EXECUTE IMMEDIATE statement
FORALL statement
COLLECT INTO clause
RETURNING INTO clause
%BULK_ROWCOUNT attribute
Oracle Database PL/SQL 10g Programming
(Chapter 13)
Page 6
Dynamic SQL
NDS: EXECUTE IMMEDIATE Clauses


INTO

Mode is OUT only.

Supports only single row return statements.
RETURNING <variable> INTO <bind_variable>



Supports name positional notation assigning variables to bind
variables.
USING

Modes are IN and OUT by itself.
Mode is IN only when concurrently using RETURNING
<variable> INTO <bind_variable> clause.

Supports only positional
notation.
Oracle Database PL/SQL 10g Programming

2006
Modes are IN and OUT.
(Chapter 13)
Page 7
Dynamic SQL
NDS: Without bind variables
DECLARE
retval
NUMBER;
statement
VARCHAR2(4000);
BEGIN
statement := 'CREATE SEQUENCE ' || a_sequence
|| 'INCREMENT BY 1';
EXECUTE IMMEDIATE statement;
END;
/
2006
Oracle Database PL/SQL 10g Programming
(Chapter 13)
Page 8
Dynamic SQL
NDS: Without substitution variables
DECLARE
retval
NUMBER;
sequence_name VARCHAR2(30) := 'sequence_name';
statement
VARCHAR2(4000);
BEGIN
statement := 'CREATE SEQUENCE '||sequence_name||' '
|| 'INCREMENT BY 1';
EXECUTE IMMEDIATE statement;
END;
/
2006
Oracle Database PL/SQL 10g Programming
(Chapter 13)
Page 9
Dynamic SQL
NDS: With bind variables
DECLARE
retval
NUMBER;
sequence_name VARCHAR2(30) := 'sequence_name';
statement
VARCHAR2(4000);
BEGIN
statement := 'CREATE SEQUENCE :sequence_name '
|| 'INCREMENT BY 1';
EXECUTE IMMEDIATE statement USING sequence_name;
END;
/
2006
Oracle Database PL/SQL 10g Programming
(Chapter 13)
Page 10
Dynamic SQL
NDS: With bind output variables
DECLARE
column_table1 VARCHAR2_NESTED_TABLE;
column_table2 VARCHAR2_NESTED_TABLE;
BEGIN
statement := 'BEGIN '
|| 'SELECT
column_name1, column_name2 '
|| 'BULK COLLECT INTO :column_table '
|| 'FROM
table_name;'
|| 'END;';
EXECUTE IMMEDIATE statement
USING OUT column_table1, OUT column_table2;
END;
/
2006
Oracle Database PL/SQL 10g Programming
(Chapter 13)
Page 11
Dynamic SQL
DBMS_SQL Built-in



2006
Enables dynamic DDL and DML SQL
statements without bind variables.
Enables dynamic DML with a known
list of bind variables.
Enables dynamic DQL.
Oracle Database PL/SQL 10g Programming
(Chapter 13)
Page 12
Dynamic SQL
DBMS_SQL Built-in: Without bind variables
DECLARE
c
INTEGER := DBMS_SQL.OPEN_CURSOR;
fdbk
INTEGER;
statement
VARCHAR2(4000);
BEGIN
statement := 'CREATE SEQUENCE ' || a_sequence
|| 'INCREMENT BY 1';
DBMS_SQL.PARSE(c,statement,DBMS_SQL.NATIVE);
fdbk := DBMS_SQL.EXECUTE(c);
DBMS_SQL.CLOSE_CURSOR(c);
END;
/
2006
Oracle Database PL/SQL 10g Programming
(Chapter 13)
Page 13
Dynamic SQL
DBMS_SQL Built-in: With bind variables
DECLARE
c
INTEGER := DBMS_SQL.OPEN_CURSOR;
fdbk
INTEGER;
input
VARCHAR2(30);
statement
VARCHAR2(4000);
BEGIN
statement := 'INSERT INTO a_table '
|| 'VALUES (:bind_variable)';
DBMS_SQL.PARSE(c,statement,DBMS_SQL.NATIVE);
DBMS_SQL.BIND_VALUE(c,bind_variable,input);
fdbk := DBMS_SQL.EXECUTE(c);
DBMS_SQL.CLOSE_CURSOR(c);
END;
/
Oracle Database PL/SQL 10g Programming
2006
(Chapter 13)
Page 14
Dynamic SQL
DBMS_SQL Built-in: With bind array variables
DECLARE
c
INTEGER := DBMS_SQL.OPEN_CURSOR;
fdbk
INTEGER;
input
VARCHAR2_NESTED_TABLE;
statement
VARCHAR2(4000);
BEGIN
statement := 'INSERT INTO a_table '
|| 'VALUES (:bind_variable)';
DBMS_SQL.PARSE(c,statement,DBMS_SQL.NATIVE);
DBMS_SQL.BIND_ARRAY(c,bind_variable,input);
fdbk := DBMS_SQL.EXECUTE(c);
DBMS_SQL.CLOSE_CURSOR(c);
END;
/
Oracle Database PL/SQL 10g Programming
2006
(Chapter 13)
Page 15
Dynamic SQL
DBMS_SQL Built-in: With output bind variables
DECLARE
c
INTEGER := DBMS_SQL.OPEN_CURSOR;
fdbk
INTEGER;
output
VARCHAR2(4000);
statement
VARCHAR2(4000);
BEGIN
statement := 'SELECT column_value FROM a_table';
DBMS_SQL.PARSE(c,statement,DBMS_SQL.NATIVE);
DBMS_SQL.DEFINE_COLUMN(c,1,output,4000);
… loop_on_next_page …
END;
/
2006
Oracle Database PL/SQL 10g Programming
(Chapter 13)
Page 16
Dynamic SQL
DBMS_SQL Built-in: With output bind variables
DECLARE
… noted_on_prior_page …
BEGIN
LOOP
EXIT WHEN DBMS_SQL.FETCH_ROWS(c) = 0;
DBMS_SQL.COLUMN_VALUE(c,1,output);
DBMS_OUTPUT.PUT_LINE('Print ['||output||']');
END LOOP;
END;
/
2006
Oracle Database PL/SQL 10g Programming
(Chapter 13)
Page 17
Summary



2006
Defining Dynamic SQL
Working with Native Dynamic SQL
Working with DBMS_SQL Built-in
Oracle Database PL/SQL 10g Programming
(Chapter 13)
Page 18
Related documents