Use of Oracle Middle-stream label (II.)

Source: Internet
Author: User

This article mainly introduces the use of cursors and several cyclic methods, including while and for as a condition for judging the loop, the loop body is used ... End LOOP. As a condition of the exit cycle there is an exit when conditions, specifically look at the following example: A: Use exit when conditions exit loop

DECLARE
    CURSOR c_emp is SELECT empid,empname,emptelep from T_emp;
    P_empid t_emp. Empid%type;
    P_empname t_emp. Empname%type;
    P_emptelep t_emp. Emptelep%type;
BEGIN
    OPEN c_emp;
    LOOP
        FETCH c_emp into P_empname,p_empid,p_emptelep;

        IF c_emp%found THEN
           dbms_output.put_line (' Employee ID is ' | | P_empid | | ', employee name is ' | | P_empname | | ', employee phone is ' | | P_EMPTELEP);
        End IF;
        Exit when c_emp%notfound;--use exit when condition, exit cycle end loop
    ;
    Close c_emp;
End;
two. Use while as a looping condition
--use while to exit the loop
DECLARE
    CURSOR c_emp is SELECT empid,empname,emptelep from T_emp;
    P_empid t_emp. Empid%type;
    P_empname t_emp. Empname%type;
    P_emptelep t_emp. Emptelep%type;
BEGIN
    OPEN c_emp;
    --Open the cursor, first to take out, to determine whether there is a value, and then cycle
    FETCH c_emp into P_empname,p_empid,p_emptelep;
    While C_emp%found
    loop   
      dbms_output.put_line (' Employee ID is ' | | P_empid | | ', employee name is ' | | P_empname | | ', employee phone is ' | | P_EMPTELEP);
      --The second value is to take the cursor out of the traversal and move to the next line
      FETCH c_emp into P_empname,p_empid,p_emptelep;      
    End LOOP;
    Close c_emp;
End;
three. Use for as a loop condition
NOTE: After Oracle 10, use for loops without open cursors, fetch cursors,  and close cursors,

declare
    CURSOR c_emp (p_id t_emp.id%type) is SELECT Empid,empname,emptelep from t_emp WHERE ID = p_id;
    P_emp C_emp%rowtype;
Begin for
    p_emp
      in C_emp loop
        dbms_output.put_line (' Employee number is ' | | P_emp. EMPID | | ', the employee's name is ' | | P_emp. EmpName | | ', the employee's phone is ' | | P_emp. EMPTELEP);
      End LOOP;
End

But, from this example, I think that for you it seems to be implicitly for you to open the cursor and fetch the cursor, when writing the For loop condition, you need to take the row object as the criterion of the loop, not a field in the line, such as you write:
For p_emp.id in 111..113
This is not a value to be taken out. Because it does not complete the implicit operation of the FETCH cursor

At this time, I was thinking, some things can not be deeply investigated, may not be able to walk, so it.

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.