In-depth high-performance dynamic Oracle SQL development (1)

Source: Internet
Author: User

Do you know about Oracle dynamic SQL development? Here is a brief introduction. You are welcome to join me in learning.

1. Static SQL and dynamic SQL

Oracle dynamic SQL development and compilation PL/SQL program blocks are divided into two types: one is the early-stage combination of early binding), that is, the SQL statement has been determined during program compilation, most compilation cases belong to this type; the other is the late binding later), that is, SQL statements can be created only in the running stage. For example, when the query condition is user input, therefore, the SQL engine of Oracle cannot determine the program statement during the compilation period. It can only be submitted to the SQL engine for processing after you enter certain query conditions. Generally, static SQL adopts the previous compiling method, while dynamic SQL Development adopts the latter compiling method.

2. Dynamic Oracle SQL Development

Understand the principles of dynamic SQL compilation, and master its basic development ideas. Since dynamic SQL is an "uncertain" SQL statement, its execution has its own characteristics. Oracle provides the Execute immediate statement to Execute dynamic SQL statements. The syntax is as follows:
Excute immediate dynamic SQL statement using binding parameter list returning into output parameter list;
This statement is described as follows:
1) dynamic SQL refers to DDL and uncertain DML, that is, DML with parameters)
2) The bound parameter list is the input parameter list, that is, the type is in. The parameters in the runtime and dynamic SQL statements are actually placeholders, which can be understood as formal parameters in the function).
3) the output parameter list is the list of parameters returned after the dynamic SQL statement is executed.
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.
To better illustrate the development process, the following example is provided:

Set the emp table of the database as follows:

ID NAME SALARY
100 Jacky 5600
101 Rose 3000
102 John 4500

Requirements:
1. Create the table and enter the corresponding data.
2. You can query the name and salary information based on the specific ID.
3. query the employee information based on the salary that exceeds the specified salary.
Based on the preceding requirements, three processes can be created using dynamic SQL:

Process 1:

 
 
  1. Create or replace procedure create_table
  2. Begin
  3. Execute immediate'
  4. Create table emp (id number,
  5. Name varchar2 (10)
  6. Salary number;) '; -- dynamic SQL is a DDL statement
  7. Insert into emp
  8. Values (100, 'jacky', 5600 );
  9. Insert into emp
  10. Values (101, 'Rose ', 3000 );
  11. Insert into emp
  12. Values (102, 'john', 4500 );
  13. End create_table;
  14.  

Process 2:

 
 
  1. Create or replace procedure find_info (p_id number)
  2. V_name varchar2 (10 );
  3. V_salary number;
  4. Begin
  5. Execute immediate'
  6. Select name, salary from emp
  7. WhereId=: 1'
  8. Using p_id
  9. Returning into v_name, v_salary; -- dynamic SQL is a query statement
  10. Dbms_output.put_line (v_name | 'revenue:' | to_char (v_salary ));
  11. Exception
  12. When others then
  13. Dbms_output.put_line ('corresponding data not found ');
  14. End find_info;
  15.  
  16. Process 3:
  17.  
  18. Create or replace procedure find_emp (p_salary number)
  19. R_emp emp % rowtype;
  20. Type c_type is ref cursor;
  21. C1 c_type;
  22. Begin
  23. Open c1'
  24. Select * from emp
  25. Where salary>: 1'
  26. Using p_salary;
  27. Loop
  28. Fetch c1 into r_emp;
  29. Exit when c1 % notfound;
  30. Dbms_output.put_line ('salary is higher than '| to_char (p_salary) |' EMPLOYEE :');
  31. Dbms_output.put_line ('Id is 'to _ char (r_emp) |' its name is '| r_emp.name );
  32. End loop;
  33. Close c1;
  34. End create_table;
  35.  

Note: In process 2, the dynamic SQL statement uses the Placeholder ": 1". In fact, it is equivalent to the form parameter of the function and uses ":" As the prefix, then use the using statement to replace p_id with: 1 at the runtime. p_id is equivalent to the real parameter in the function. In addition, the cursor opened in process 3 is a dynamic cursor, which also belongs to the category of dynamic SQL. The entire compilation and development process is similar to the execute immediate execution process, which is not described here.


Related Article

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.