How to use dynamic query statements in Oracle

Source: Internet
Author: User

Oracle dynamic query statements are a special type of query statements. The following describes the syntax of Oracle dynamic query statements in detail. If you are interested in Oracle dynamic query, take a look.

1. When you use the execute immediate statement to process a single row query, you must use the INTO clause to accept the returned data:

 
 
  1. DECLARE
  2.  
  3. Query_stat VARCHAR2 (100): = 'select * FROM emp' | 'where empno =: eno ';
  4.  
  5. Emp_record emp % ROWTYPE;
  6.  
  7. BEGIN
  8.  
  9. Execute immediate query_stat INTO emp_record USING & eno;
  10.  
  11. Dbms_output.put_line ('name: '| emp_record.ename |', position: '| emp_record.job );
  12.  
  13. END;
  14.  

2. execute immediate can only be used to process single-row queries. to dynamically process multiple rows of data returned by the SELECT statement, you need to define the cursor variable and use the OPEN-FOR, FETCH, and CLOSE statements to complete it together. The procedure is as follows:

#1 Define the cursor variable: Because you need to use the cursor variable to dynamically process multi-row queries, You need to define the cursor variable in the definition section. The syntax is as follows:

 
 
  1. TYPE cursortype IS REF CURSOR;  
  2.  
  3.                 cursor_variable cursortype;  

#2 open the cursor variable: When the cursor variable is opened, the dynamic SELECT statement corresponding to the cursor variable is executed, and the query results are stored in the cursor result set. Syntax:

 
 
  1. OPEN cursor_variable FOR dynamic_string  
  2.  
  3.                              [USING bind_argument];  

#3 extract data: extract data to store row data in the result set to PL/SQL variables. The syntax is as follows:

 
 
  1. FETCH cursor_variable INTO {VAR1}; 

#4 close the cursor variable: Close the cursor variable to release the cursor result set. The syntax is as follows:

 
 
  1. CLOSE cursor_variable; 

Example:

 
 
  1. DECLARE
  2.  
  3. TYPE empcurtyp is ref cursor;
  4.  
  5. Emp_cv empcurtyp;
  6.  
  7. Emp_record emp % ROWTYPE;
  8.  
  9. SQL _stat VARCHAR2 (100 );
  10.  
  11. BEGIN
  12.  
  13. SQL _stat: = 'select * FROM emp WHERE deptno =: dno ';
  14.  
  15. OPEN emp_cv FOR SQL _stat USING & dno;
  16.  
  17. LOOP
  18.  
  19. FETCH emp_cv INTO emp_record;
  20.  
  21. Exit when emp_cv % NOTFOUND;
  22.  
  23. Dbms_output.put_line ('employee name: '| emp_record.ename |', salary: '| emp_record.sal );
  24.  
  25. End loop;
  26.  
  27. CLOSE emp_cv;
  28.  
  29. END;
  30.  

The preceding section describes the usage of Oracle dynamic query statements.

Implementation of oracle query current time

Learn more about Oracle hierarchical Query

Provides you with an in-depth understanding of Oracle temporary tables

Oracle with statement usage

Common ORACLE Data Types

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.