Tuesday, 7 August 2012

Example for VARRAYS and RECORD types in PL/SQL

Here is small code snippet which illustrates the VARRAYS and RECORD types in PL/SQL.

This is just for reference purpose :

----------------------------------------
--   Test Script
----------------------------------------
SET SERVEROUTPUT ON SIZE 1000000
SET VERIFY OFF
SET FEEDBACK OFF
SET TERMOUT OFF

SPOOL C:\Results.txt

DECLARE

    TYPE users IS RECORD(
            p_invoice_id          NUMBER,
            p_approver           VARCHAR2(250),
            p_status               VARCHAR2(250)
            );
           
    TYPE number_of_users IS VARRAY(50) OF users;
   
    p_inputs    number_of_users  := number_of_users();
   
BEGIN
     p_inputs.extend(3);
   
     p_inputs(1).p_invoice_id :=  1111;
     p_inputs(1).p_approver  := 'AAAAAA';
     p_inputs(1).p_status := 'Active';

     p_inputs(2).p_invoice_id :=  2222;
     p_inputs(2).p_approver  := 'BBBB';
     p_inputs(2).p_status := 'Active';

     p_inputs(3).p_invoice_id :=  3333;
     p_inputs(3).p_approver  := 'CCCC';
     p_inputs(3).p_status := 'Active';

     FOR i in 1..p_inputs.count
     LOOP
    
        DBMS_OUTPUT.PUT_LINE('Test Condition : '|| i );
        DBMS_OUTPUT.PUT_LINE(CHR(13)||CHR(10));
        DBMS_OUTPUT.PUT_LINE('Input Parameters : ');
       
        DBMS_OUTPUT.PUT_LINE('Invoice ID :'||p_inputs(i).p_invoice_id);
        DBMS_OUTPUT.PUT_LINE('Approver :'||p_inputs(i).p_approver);
        DBMS_OUTPUT.PUT_LINE('Status :'||p_inputs(i).p_status);      
      
     END LOOP;  

END;
/
SPOOL OFF

No comments:

Post a Comment