1. Static SQL
Static SQL statements are SQL statements directly embedded in PL/SQL blocks. When compiling PL/SQL. Static SQL is used to complete specific or fixed tasks:
2. dynamic SQL
Dynamic SQL is an SQL statement that is dynamically entered when PL/SQL blocks are run. If you need to execute DDL statements in PL/SQL, DCL statements (Grant, revoke ), or dynamic SQL must be used to execute more flexible SQL statements in PL/SQL (for example, using different where conditions in select statements.
When writing dynamic statements in PL/SQL blocks, you need to store the SQL statements in character creation variables, and the SQL statements can contain placeholders (starting with a colon ).
For example: Create Table temp (COLA int, COLB varchar2 (10 ))
Grant select on temp to Smith
Delete from EMP where SAL>:
Select ename, Sal from EMP where empno =: l
3. Handling of dynamic SQL statements
(1) execute immediate
It cannot be used to process multi-row query statements.
(2) Use the Open-for Fetch and close statements
To process dynamic multi-row queries, you must use the open-for statement to open the cursor, use the fetch statement to extract cyclic data, and use the close statement to close the cursor.
(3) Use batch dynamic SQL
Process non-query statements
To dynamically process non-query statements (dml ddl dcl) or single-row query statements, you can use the exexexute immediate statement in PL/SQL quick,
Execute immediate dynamic_string
[Into | define_variable |, define_variable... | Record]
[Using [in | Out | inout] bind_argument...]
[{Returning | return} into bind_argument...];
Dynamic_string is used to specify the string variables that store SQL statements or PL/SQL blocks;
Define_variable is used to specify the variable used to store a single row to query the marriage.
Enter bind_argument (in) to specify the variables that are passed to the dynamic SQL value.
The output bind_argument (out) is used to specify the variable for storing the dynamic SQL return value.
1. Execute immediate to process DDL operations
Execute immediate must be followed by DDL statement text instead of into and using.
Words.
Create or replace procedure drop_table (table_name varchar2)
Is
SQL _statement varchar2 (100 );
Begin
SQL _statement: = 'drop table' | table_name;
Execute immediate SQL _statement;
End;
2. Execute immediate for DCL operations
Similar to processing DDL statements
3. Execute immediate to process DML statements
If a DML statement has no placeholders or returning clauses, the execute immediate statement does not need to contain the using and returning into clauses;
If the DML statement contains placeholders, The execute immediate statement must be followed by the using clause;
If a DML statement is followed by a returning clause, the execute immediate statement is followed by a returning into clause.
(1) process DML statements without placeholders and returning clauses
Declare
SQL _statement varchar2 (100 );
Begin
SQL _statement: = 'Update EMP set sal = Sal * 1.1 where depno = 30 ';
Execute immediate SQL _statement;
End;
(2) process DML statements containing placeholders
You need to use using to provide input data for placeholders by yourself.
Declare
SQL _state varchar2 (100 );
Begin
SQL _state: = 'Update EMP set sal = Sal * (1 +: percent/100) WHRE deptno =: dno ';
Execute immediate SQL _state using & 1, & 2;
End;
(3) process DML statements containing the returning clause
It is necessary to use the returning into clause to accept the returned data.
Note: When you directly use the execute immediate statement to process DML statements with a returning clause, you can only process DML statements that work in a single row.
If the DML statement is affected by multiple rows, the bulk clause must be used.
Declare
Salary number (6, 2 );
SQL _state varchar2 (100 );
Begin
SQL _state: = 'Update EMP set sal = (Sal *: Present/100) Where empno =: Eno returning Sal into: Salary ';
Execute immediate SQL _state using & 1, & 2 returning into salary;
Dbms_output.put_line ('new salary: '| salary );
End;
(4) execute immediate to process a single row Query
Use the into clause to accept returned data
Declare
SQL _state varchar2 (100 );
Emp_record EMP % rowtype;
Begin
SQL _state: = 'select * from EMP where empno =: eno ';
Execute immediate SQL _state into emp_record using & 1;
Dbms_output.put_line ('employee: '| emp_record.ename |' salary: '| emp_record.sal );
End;
Multi-line query statement processing:
Execute immediate can only be used to process single-row query statements. to dynamically process multiple rows of data returned by a select statement, open-for, fetch, and close statements are required.
The procedure is as follows:
Define the cursor variable --> open the cursor variable --> extract data cyclically --> close the cursor variable.
1. Define the cursor variable:
Type cursortype is ref cursor;
Cursor_variable cursortype;
2. Call cursor variable
Open cursor_variable for dynamic_string [using bind_argument,...];
Bind_argument is used to store the variables passed to the dynamic SELECT statement value.
3. extract data cyclically
Fetch cursor_variable into {var1 [, var2]... | record_var };
4. Close the cursor variable
Close cursor_variable;
5. multi-row query instance:
Declare
Type cursor_type is ref cursor;
SQL _state varchar2 (100 );
Emp_record EMP % rowtype;
V_cursor vursor_type;
Begin
SQL _state: = 'select * from EMP where deptno =: dno ';
Open v_cursor for SQL _state using & deptno;
Loop
Fetch v_cursor into emp_record;
Exit when v_cursor % notfound;
Dbms_output.put_line ('employee name: '| emp_record.ename |' salary: '| emp_record.salary );
End loop;
Close v_cursor;
End;
Use the bulk clause in dynamic SQL
New Features of Oracle9i. This can accelerate the processing of batch data.
Three statements support bulk clauses: execute immediate, fetch, and forall.
1. Use the dynamic bulk clause in the execute immediate statement
Execute immediate dynamic_string
[Bulk collect into | define_variable |, define_variable... | Record]
[Using [in | Out | inout] bind_argument...]
[{Returning | return} bulk collect into bind_argument...];
(1) Use the bulk clause to process the return clause of DML statements
Declare
Type ename_table_type is table of EMP. ename % Type Index by binary_integer;
Type sal_table_type is table of EMP. Sal % Type Index by binary_integer;
Ename_table ename_table_type;
Sal_table sal_table_type;
SQL _state varchar2 (150 );
Begin
SQL _state: = 'Update EMP set sal = Sal * (1 +: precent/100) Where deptno =: dno'
| 'Returning ename, Sal into: name,: Salary ';
Execute immediate SQL _state using & precent, & depno returning bulk collect
Ename_table, sal_table;
For I in 1 .. ename_table.count Loop
Dbms_output.put_line ('employee name: '| ename_table (I) |' salary: '| sal_table (I ));
End loop;
End;
(2) Use the bulk clause to process multi-row queries
Declare
SQL _state varchar2 (100 );
Type ename_table_type is table of EMP. ename % Type Index by binary_integer;
Ename_table ename_table_type;
Begin
SQL _state: = 'select ename from EMP where empno =: eno ';
Execute immediate SQL _state bulk collect into emp_record using & 1;
For I in 1 .. ename_table.count Loop
Dbms_output.put_line ('employee: '| ename_table (I ));
End loop;
End;
2. Use the bulk clause in the fetch statement
Open-for, fetch, close... bulk
Declare
Type cursor_type is ref cursor;
Type ename_table_type is table of EMP. ename % Type Index by binary_integer;
SQL _state varchar2 (200 );
V_vursor cursor_type;
Ename_table ename_table_type;
Begin
SQL _state: = 'select ename from EMP where deptno =: dno ';
Open v_corsor for SQL _state using & deptno;
Fetch v_cursor bulk collect into ename_table;
For I in I... ename_table.count Loop
Dbms_output.put_line ('employee: '| ename_table (I ));
End loop;
Close v_cursor;
End;
Forall