How to check if cursor returns any records in oracle?

后端 未结 5 1902
旧时难觅i
旧时难觅i 2020-12-11 02:41

I have a following stored procedure in which I have used a cursor. Depending on whether the cursor return any records or not I need to do some processing.

But I am

相关标签:
5条回答
  • 2020-12-11 02:51

    341/5000 One solution would be to work with reverse logic. It is e.g. not possible to ask:

    IF my_record IS NULL THEN
       do something;
    END IF;
    

    or

    IF my_record.my_attibute IS NULL THEN
       do something;
    END IF;
    

    Instead it is possible to ask:

    IF my_record.my_attibute IS NOT NULL THEN
       go on processing;
    ELSE
       do something;
    END IF;
    
    0 讨论(0)
  • 2020-12-11 03:07

    I like this way:

    DECLARE
    
        CURSOR my_cur
        IS
        SELECT 'a' AS a FROM DUAL WHERE 1=1;
    
        my_rec my_cur%rowtype;
    
    BEGIN
        OPEN my_cur;
        LOOP
    
            FETCH my_cur INTO my_rec;
    
            IF my_cur%NOTFOUND
            THEN
                IF my_cur%ROWCOUNT=0
                THEN
                    -- do stuff when cursor empty
                    DBMS_OUTPUT.PUT_LINE('NOTFOUND,RC=0' || '_' || my_cur%ROWCOUNT || '_' || my_rec.a);
                END IF;
                EXIT;
            ELSE
                    -- do stuff when cursor not empty
                DBMS_OUTPUT.PUT_LINE('FOUND,RC>0' || '_' || my_cur%ROWCOUNT || '_' || my_rec.a);
            END IF;
    
        END LOOP;
        CLOSE MY_CUR;
    END;
    

    Output when cursor is 1=1 (not empty):

    FOUND,RC>0_1_a
    

    Output when cursor is 1=0 (empty):

    NOTFOUND,RC=0_0_
    
    0 讨论(0)
  • 2020-12-11 03:13

    It's not possible to check if the cursor returns records without opening it.
    (see here)
    So you can either have some fast query just to see if there are records (using count for example),

    Or, you can do it like this:

    CREATE OR REPLACE PROCEDURE SP_EMPLOYEE_LOOKUP_BY_EMP_ID
    (
          IN_USER_ID IN NUMBER, 
          IN_EMPLOYEE_ID NUMBER,
          IN_HC_AS_ON_DATE VARCHAR2,
          emp_cursor OUT SYS_REFCURSOR
    ) 
    IS 
    
     is_found_rec boolean := false;    
    
     CURSOR employees IS 
        SELECT  * FROM EMPLOYEE e; 
    
    BEGIN    
    
     FOR employee IN employees
      LOOP  
    
        is_found_rec := true;
    
            // do something  
    
      END LOOP; 
    
     if not is_found_rec then 
         // do something else 
     end if;
    
    END;
    
    0 讨论(0)
  • 2020-12-11 03:16

    I think that it possible only with FETCH. Try to use

    if myCursor%found then
    // some body
    end if;
    

    But if somebody know another way so correct me.

    0 讨论(0)
  • 2020-12-11 03:16

    I like to use the same select query that comprises the cursor and select the count it returns into a variable. You can then evaluate the variable to determine what to do next. For example you have a cursor like this:

    CURSOR c_example IS 
        SELECT field1
            ,field2
            ,field3
        FROM table1 a
        WHERE a.field1 LIKE '%something%';
    

    Before you open the cursor to do any processing, select cursor result count into a variable.

    SELECT count(*)
    INTO v_exampleCount
    FROM table1 a
    WHERE a.field1 LIKE '%something%';
    

    Now, again before you open cursor, do something like:

    IF exampleCount>0 THEN
    
        FOR example IN c_example
        LOOP
            ....
        END LOOP;  --end example loop
    
        ....
    
    END IF;  --end example if
    
    0 讨论(0)
提交回复
热议问题