Oracle cursor opening and closing

Source: Internet
Author: User
Tags oracle cursor

The following describes Oracle cursors. This article describes how to enable the Oracle cursor and the actual operations you want to perform. If you are interested in the actual operations, the following articles will provide you with relevant knowledge.

Open Oracle cursor

Before using the values in the cursor, dewengz should first open the cursor and open the cursor to initialize query processing. The syntax for opening a cursor is:

 
 
  1. OPEN cursor_name  

Cursor_name is the cursor name defined in the Declaration section.

Example:

 
 
  1. OPEN C_EMP;  

Close Oracle cursor

Syntax:

 
 
  1. CLOSE cursor_name  

Example:

 
 
  1. CLOSE C_EMP;  

Close Oracle cursor

Use the FETCH Command to obtain a row of data from the cursor. After each data extraction, the cursor points to the next row of the result set. Syntax:

 
 
  1. FETCH cursor_name INTO variable[,variable,...]  

For each column of the Oracle cursor defined by SELECT, The FETCH Variable list should have a variable corresponding to it, and the variable type should be the same.

Example:

 
 
  1. SET SERVERIUTPUT ON  
  2. DECLARE  
  3. v_ename EMP.ENAME%TYPE;  
  4. v_salary EMP.SALARY%TYPE;  
  5. CURSOR c_emp IS SELECT ename,salary FROM emp;  
  6. BEGIN  
  7. OPEN c_emp;  
  8. FETCH c_emp INTO v_ename,v_salary;  
  9. DBMS_OUTPUT.PUT_LINE('Salary of Employee'|| v_ename  
  10. ||'is'|| v_salary);  
  11. FETCH c_emp INTO v_ename,v_salary;  
  12. DBMS_OUTPUT.PUT_LINE('Salary of Employee'|| v_ename  
  13. ||'is'|| v_salary);  
  14. FETCH c_emp INTO v_ename,v_salary;  
  15. DBMS_OUTPUT.PUT_LINE('Salary of Employee'|| v_ename  
  16. ||'is'|| v_salary);  
  17. CLOSE c_emp;  
  18. END  

This code is undoubtedly very troublesome. If multiple rows return results, you can use the loop and use the Oracle cursor attribute as the condition for ending the loop to extract data in this way, the readability and conciseness of the program are greatly improved. Next we will re-write the following program in a loop:

 
 
  1. SET SERVERIUTPUT ON  
  2. DECLARE  
  3. v_ename EMP.ENAME%TYPE;  
  4. v_salary EMP.SALARY%TYPE;  
  5. CURSOR c_emp IS SELECT ename,salary FROM emp;  
  6. BEGIN  
  7. OPEN c_emp;  
  8. LOOP  
  9. FETCH c_emp INTO v_ename,v_salary;  
  10. EXIT WHEN c_emp%NOTFOUND;  
  11. DBMS_OUTPUT.PUT_LINE('Salary of Employee'|| v_ename  
  12. ||'is'|| v_salary);  
  13. END  

The above content describes the specific Oracle cursor operations, hoping to help you in this regard.

Article by: http://www.programbbs.com/doc/class10-3.htm

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.