简体   繁体   中英

How to discover the index type of an Oracle 12c associative array from the dictionary views?

Using Oracle 12c, I have the following PL/SQL package

CREATE OR REPLACE PACKAGE x AS
  TYPE t IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
  FUNCTION f RETURN t;
END x;
/

I would now like to dynamically discover the return type of the above function f :

SELECT *
FROM all_arguments
WHERE package_name = 'X';

This query yields something along the lines of

在此处输入图片说明

... which can be parsed into a useful data structure using a fancy recursive query, just in case the table contains records, not numbers (eg like the one in this article for PL/SQL RECORD ).

What I couldn't find in the dictionary views, however, is a formal reference to the index type of the associative array, ie PLS_INTEGER . Is there any way to discover that in a different view?

Note, I'd like to avoid parsing the contents of ALL_IDENTIFIERS , as I cannot rely on PLSCOPE_SETTINGS='IDENTIFIERS:ALL' on any target systems.

Please forgive me for I have sinned:

WITH
  FUNCTION index_type(type_owner VARCHAR2, type_name VARCHAR2, type_subname VARCHAR2) 
  RETURN VARCHAR2 IS
    l_result VARCHAR2(50) := 'UNKNOWN';
  BEGIN
    EXECUTE IMMEDIATE q'[
      DECLARE
        l_result VARCHAR(50);
        v "]' || type_owner || '"."' || type_name || '"."' || type_subname || q'[";
      BEGIN
        BEGIN
          v('A') := NULL;
          l_result := 'VARCHAR2';
        EXCEPTION
          WHEN OTHERS THEN
            BEGIN
              v(1) := NULL;
              l_result := 'PLS_INTEGER';
            EXCEPTION
              WHEN OTHERS THEN l_result := 'UNKNOWN';
            END;
        END;

        :result := l_result;
      END;]' USING OUT l_result;

    RETURN l_result;
  EXCEPTION
    WHEN OTHERS THEN RETURN SQLERRM;
  END index_type;
SELECT 
  type_owner, type_name, type_subname,
  index_type(type_owner, type_name, type_subname) AS index_type
FROM all_arguments
WHERE data_type = 'PL/SQL TABLE';

Which yields the desired result:

在此处输入图片说明

Of course, the type definition deserves more details, including length, precision, etc, but this is good enough for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM