Oracle PL/SQL - Are NO_DATA_FOUND Exceptions bad for stored procedure performance?

后端 未结 12 844
Happy的楠姐
Happy的楠姐 2021-02-03 23:14

I\'m writing a stored procedure that needs to have a lot of conditioning in it. With the general knowledge from C#.NET coding that exceptions can hurt performance, I\'ve always

12条回答
  •  不知归路
    2021-02-04 00:12

    An alternative to @Steve's code.

    DECLARE
      CURSOR foo_cur IS 
        SELECT NEEDED_FIELD WHERE condition ;
    BEGIN
      FOR foo_rec IN foo_cur LOOP
         ...
      END LOOP;
    EXCEPTION
      WHEN OTHERS THEN
        RAISE;
    END ;
    

    The loop is not executed if there is no data. Cursor FOR loops are the way to go - they help avoid a lot of housekeeping. An even more compact solution:

    DECLARE
    BEGIN
      FOR foo_rec IN (SELECT NEEDED_FIELD WHERE condition) LOOP
         ...
      END LOOP;
    EXCEPTION
      WHEN OTHERS THEN
        RAISE;
    END ;
    

    Which works if you know the complete select statement at compile time.

提交回复
热议问题