Friday, 24 January 2014

Bulk collect requirement

DECLARE

TYPE rec_inv IS RECORD
                      (
                       invoice_id                ap_invoices_all.invoice_id%type,
                       invoice_num               ap_invoices_all.invoice_num%type,
                       Invoice_currency_code     ap_invoices_all.Invoice_currency_code%type,
                       payment_currency_code     ap_invoices_all.payment_currency_code%type,
                       invoice_amount            ap_invoices_all.invoice_amount%type,
                       vendor_site_id            ap_invoices_all.vendor_site_id%type,
                       invoice_type_lookup_code  ap_invoices_all.invoice_type_lookup_code%type,                      
                       lv_error_message          varchar2(4000)
                       );
TYPE typ_inv IS TABLE OF rec_inv INDEX BY PLS_INTEGER;

ap_inv_standard   typ_inv;


l_error_count  NUMBER;

l_cnt  NUMBER := 0;
 
ex_dml_errors   EXCEPTION;
 
PRAGMA EXCEPTION_INIT(ex_dml_errors, -24381);

BEGIN
       SELECT         invoice_id,
                      invoice_num,
                      Invoice_currency_code,
                      payment_currency_code,
                      invoice_amount,
                      VENDOR_SITE_ID,
                      INVOICE_TYPE_LOOKUP_CODE,
                      NULL                    
       BULK COLLECT
               INTO  ap_inv_standard
               FROM 
                      AP_INVOICES_ALL ;

    FOR i IN 1..ap_inv_standard.COUNT
  LOOP
      IF ap_inv_standard(i).INVOICE_TYPE_LOOKUP_CODE = 'STANDARD' THEN
       
         NULL;
                                                      
         ELSE                
       ap_inv_standard(i).lv_error_message := 'Invoice Number '||ap_inv_standard(i).invoice_num||' is Not Standard Invoice Type' ;
      
       dbms_output.put_line(ap_inv_standard(i).lv_error_message);
       l_cnt := l_cnt+1;
      
      
          END IF;
         
   END LOOP;        
            
          dbms_output.put_line(' Not Standard Type Count :- '||l_cnt);
                                 
          FORALL j IN indices OF  ap_inv_standard SAVE EXCEPTIONS
         
      
            INSERT INTO xxap_invoices_all
             SELECT ap_inv_standard(j).invoice_id,
                                                 ap_inv_standard(j).invoice_num,
                                                 ap_inv_standard(j).Invoice_currency_code,
                                                 ap_inv_standard(j).payment_currency_code,
                                                 ap_inv_standard(j).invoice_amount,
                                                 ap_inv_standard(j).VENDOR_SITE_ID,
                                                 ap_inv_standard(j).INVOICE_TYPE_LOOKUP_CODE
               FROM DUAL
              WHERE ap_inv_standard(j).lv_error_message IS NULL;
     
   EXCEPTION
 
    WHEN ex_dml_errors THEN
   
      l_error_count := SQL%BULK_EXCEPTIONS.COUNT;
     
      DBMS_OUTPUT.put_line('Number of failures: ' || l_error_count);
     
         FOR l IN 1 .. l_error_count LOOP
        DBMS_OUTPUT.put_line('Error: ' || l ||
          ' Array Index: ' || SQL%BULK_EXCEPTIONS(l).error_index ||
          ' Message: ' || SQLERRM(-SQL%BULK_EXCEPTIONS(l).ERROR_CODE));
            END LOOP;
     
   COMMIT;
     
  END;

No comments:

Post a Comment