How to check if cursor returns any records in oracle?

后端 未结 5 1909
旧时难觅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 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_
    

提交回复
热议问题