How to check if cursor returns any records in oracle?

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

提交回复
热议问题