Three methods of writing oracle dynamic statements

Source: Internet
Author: User

There are two ways to write SQL statements:

 

Oracle compiler PL/SQL program blocks are divided into two types: one is the early compilation (early binding), that is, the SQL statement has been determined during program compilation, and most of the compilation situations belong to this type; the other is late binding)

4) Because dynamic SQL statements are determined at runtime, compared with static SQL statements, it will lose some system resources in exchange for its flexibility.

 

Static and dynamic operations are only different in terms of pre-and post-joint editing.

Such as object information, syntax check, object check

 

Three writing methods

Fixed SQL, variable SQL binding, and hard-coded SQL

Dynamic
1 SQL = 'select * from table where name = 'Lee 3''
2 SQL = 'select * from table where name =: p_name'
3 SQL = 'select * from table where name = '| p_name;

 

Declare
Vc_ SQL varchar2 (1000 );
I number;
Begin

I: = 10;
Vc_ SQL: = 'select/* + demo 1 */* from t where object_id = 10 ';
Execute immediate vc_ SQL;

Vc_ SQL: = 'select/* + DEMO 2 */* from t where object_id =: 1 ';
Execute immediate vc_ SQL using I;

Vc_ SQL: = 'select/* + demo 3 */* from t where object_id = '| I;
Execute immediate vc_ SQL;

I: = 20;
Vc_ SQL: = 'select/* + demo 1 */* from t where object_id = 10 ';
Execute immediate vc_ SQL;

Vc_ SQL: = 'select/* + demo 2 */* from t where object_id =: 1 ';
Execute immediate vc_ SQL using I;

Vc_ SQL: = 'select/* + demo 3 */* from t where object_id = '| I;
Execute immediate vc_ SQL;

End;
/

 

SQL> select SQL _text from v $ sqlarea where SQL _text like 'select/* + demo % 'order by SQL _text;

SQL _TEXT
------------------------------
Select/* + demo 1 */* from t where object_id = 10

Select/* + demo 1 */* from t where object_id = 20

Select/* + demo 2 */* from t where object_id =: 1

Select/* + demo 3 */* from t where object_id = 10

Select/* + demo 3 */* from t where object_id = 20

If the repeated execution conditions remain unchanged, the three methods can be reused,

If conditions change,

The first type of literal writing can be reused,

The second type of variable binding can basically reuse ORACLE 10 Gb later, it will peat the value of the variable. If the data distribution information is stored, the execution plan may be regenerated.

The third parameterization will not be reused because the condition value changes,

The static SQL statement only removes the quotation marks, and the execute immediate statement is basically the same as the preceding statement.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.