如何编写返回关联数组的函数并在Oracle PLSQL中调用该函数?

问题描述 投票:0回答:2

如何在 Oracle PL/SQL 中声明和定义返回关联数组的函数? 以及如何从过程中调用相同的函数?

oracle plsql plsqldeveloper
2个回答
1
投票

以下是如何操作的示例:

DECLARE
    TYPE my_associative_array_type IS TABLE OF VARCHAR2(100) INDEX BY PLS_INTEGER;
    
    FUNCTION return_associative_array RETURN my_associative_array_type IS
        my_array my_associative_array_type;
    BEGIN
        -- Populate the associative array
        my_array(1) := 'First Element';
        my_array(2) := 'Second Element';
        -- ... add more elements as needed
        RETURN my_array;
    END return_associative_array;

    PROCEDURE use_associative_array IS
        local_array my_associative_array_type;
    BEGIN
        -- Call the function
        local_array := return_associative_array;

        -- Use the array
        DBMS_OUTPUT.PUT_LINE('First Element: ' || local_array(1));
        DBMS_OUTPUT.PUT_LINE('Second Element: ' || local_array(2));
        -- ... process other elements or iterate over them
    END use_associative_array;

BEGIN
    -- Call the procedure
    use_associative_array;
END;

或者您也可以使用包:

CREATE OR REPLACE PACKAGE my_package IS
    TYPE my_associative_array_type IS TABLE OF VARCHAR2(100) INDEX BY PLS_INTEGER;

    FUNCTION return_associative_array RETURN my_associative_array_type;

    PROCEDURE use_associative_array;
END my_package;

封装体

CREATE OR REPLACE PACKAGE BODY my_package IS

    FUNCTION return_associative_array RETURN my_associative_array_type IS
        my_array my_associative_array_type;
    BEGIN
        my_array(1) := 'First Element';
        my_array(2) := 'Second Element';
        -- ... add more elements as needed
        RETURN my_array;
    END return_associative_array;

    PROCEDURE use_associative_array IS
        local_array my_associative_array_type;
    BEGIN
        local_array := return_associative_array;
        DBMS_OUTPUT.PUT_LINE('First Element: ' || local_array(1));
        DBMS_OUTPUT.PUT_LINE('Second Element: ' || local_array(2));
        -- ... process other elements or iterate over them
    END use_associative_array;

END my_package;

使用套件

BEGIN
    -- Call the procedure from the package
    my_package.use_associative_array;
END;

-1
投票

--包裹申报 创建或替换 PACKAGE myinv AS

       TYPE namevalue_type IS TABLE OF VARCHAR2(100) INDEX BY VARCHAR2(50);
       Procedure Test ( params IN varchar2 );
       Function ParseParams ( params IN varchar2) RETURN namevalue_type;
        
END myinv ; 
/




--Definition of the package
CREATE OR REPLACE PACKAGE BODY myinv AS  
--   TYPE namevalue_type IS TABLE OF VARCHAR2(100) INDEX BY VARCHAR2(50);


   -- Function which does the parsing and returns associative array
   Function ParseParams ( params IN varchar2) 
   RETURN namevalue_type 
   IS
      nv_pairs SYS.ODCIVARCHAR2LIST;
      nv_pair  SYS.ODCIVARCHAR2LIST;
      i number;
      j number;
      name varchar2(100);
      value varchar2(100);
      nv_t namevalue_type;

   BEGIN 
   
         SELECT regexp_substr(params,'(.*?)(;|$)', 1, level, NULL, 1)
          BULK COLLECT INTO nv_pairs
          FROM   dual
          CONNECT BY level <= regexp_count(params, ';') + 1;
        
          i := nv_pairs.FIRST;  -- Get first element of array
          while i is not null LOOP
                  
              SELECT regexp_substr(nv_pairs(i),'(.*?)(=|$)', 1, level, NULL, 1)
              BULK COLLECT INTO nv_pair
              FROM   dual
              CONNECT BY level <= regexp_count(nv_pairs(i), '=') + 1;
              
              j := nv_pair.FIRST;
              while j is not null LOOP
                name := nv_pair(j);
                j := nv_pairs.NEXT(j); 
                value := nv_pair(j);
                nv_t(name) := value;
                j := nv_pairs.NEXT(j); 
              END LOOP;
              
             i := nv_pairs.NEXT(i);  -- Get next element of array      
          END LOOP;
          
          name := nv_t.FIRST;
          WHILE name IS NOT NULL LOOP
            name := nv_t.NEXT(name);
          END LOOP;
    
        RETURN nv_t;

   END ParseParams; 
   
   --Procedure to test the function
    Procedure Test ( params IN varchar2 )
    AS
    nvt namevalue_type;
    name varchar2(100);
    i number;
    BEGIN
    dbms_output.put_line('  Inside Procedure Test');
        i := 1;
        nvt := ParseParams('param1=10;param2=20');
        name := nvt.FIRST;
        WHILE name IS NOT NULL LOOP
          --dbms_output.put_line('            ' || i ||'. Name: ' || name || '  Value: ' || nvt(name) );
          dbms_output.put_line('            ' || i ||'. Name: ' || name || '  Value: ' || (to_number(nvt(name)) + 10) );
          name := nvt.NEXT(name);
          i := i+1;
        END LOOP;
          
          
    END Test;
END myinv; 
/


--Run the test
BEGIN
  DBMS_OUTPUT.PUT_LINE('Test Parse.........................');
  myinv.Test('Check');
END;
/
© www.soinside.com 2019 - 2024. All rights reserved.