When should I nest PL/SQL BEGIN…END blocks?

前端 未结 3 852
独厮守ぢ
独厮守ぢ 2020-12-28 18:41

I\'ve been somewhat haphazardly grouping subsections of code in BEGIN...END blocks when it seems right. Mostly when I\'m working on a longer stored procedure and there\'s a

3条回答
  •  梦毁少年i
    2020-12-28 19:30

    I tend to nest blocks when I want to create procedures that are specific to data that only exists within the block. Here is a contrived example:

    BEGIN
      FOR customer IN customers LOOP
        DECLARE
    
          PROCEDURE create_invoice(description VARCHAR2, amount NUMBER) IS
          BEGIN
            some_complicated_customer_package.create_invoice(
                customer_id => customer.customer_id,
                description => description,
                amount => amount
              );
          END;
    
        BEGIN
    
          /* All three calls are being applied to the current customer,
             even if we're not explicitly passing customer_id.
           */
          create_invoice('Telephone bill',  150.00);
          create_invoice('Internet bill',   550.75);
          create_invoice('Television bill', 560.45);
    
        END;
      END LOOP;
    END;
    

    Granted, it's not usually necessary, but it has come in really handy when a procedure can be called from many locations.

提交回复
热议问题