Oracle cursor usage methods three kinds of loops ____oracle

Source: Internet
Author: User
Example: Cursor loop loop cursor SET serveriutput on DECLARE r_emp emp%rowtype;   CURSOR C_emp is a SELECT * from EMP;   BEGIN OPEN c_emp;   LOOP FETCH c_emp into r_emp;   EXIT when C_emp%notfound; Dbms_out. Put. Put_Line (' Salary of Employee ' | | r_emp.ename| | ' Is ' | |   R_emp.salary);   End LOOP;   Close c_emp; End;
Cursors with parameters
DECLARE cursor c_columns (t_table_name varchar) is select column_name from user_tab_columns where table_name = T_table_name ;
BEGIN Open c_columns (' table_name '); Fetch c_columns into V_column; If C_columns%found then Dbms_output.put_line (' column name: ' | | V_column); End If; Close C_columns; End;

Second: Use while loop open C1; Fetch C1 into v_name,v_job; While C1%found loop ... fetch C1 into v_name,v_job; End Loop; Close C1; Note that two FETCH statements are used here. The first FETCH statement assigns the first row of the cursor to the related variable, and the second FETCH statement is to have the cursor point to the next record. If there is no second statement, it is a dead loop, and the code inside the loop is executed non-stop. In summary, using a while to iterate over a cursor is the most complex method.
Third: For loops for V_pos in C1 loop v_name:= v_pos.ename; V_job:= V_pos.ejob; . end Loop; A For loop is a relatively simple and practical method. First, it will automatically open and close cursors. Resolves the annoyance of your forgetting to turn on or off the cursor. Other, automatically defines a record type and declares the variable of that type, and automatically fetch the data into this variable. We need to be aware that v_pos this variable does not need to be declared outside the loop, without having to specify a data type for it. It should be a record type, and the specific structure is determined by the cursor. The scope of this variable is only in the loop body. It's OK to think of V_pos as a record variable if you want to get a value that's like calling a record. As V_pos.pos_type this shows that the For loop is the best way to loop a cursor. efficient, concise and safe.

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.