简体   繁体   English

如何从字典视图中发现 Oracle 12c 关联数组的索引类型?

[英]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使用 Oracle 12c,我有以下 PL/SQL 包

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 :我现在想动态发现上述函数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 ). ...可以使用花哨的递归查询将其解析为有用的数据结构,以防万一表包含记录,而不是数字(例如,就像本文中针对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 .然而,我在字典视图中找不到的是对关联数组的索引类型的正式引用,即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.请注意,我想避免解析ALL_IDENTIFIERS的内容,因为我不能在任何目标系统上依赖PLSCOPE_SETTINGS='IDENTIFIERS:ALL'

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.当然,类型定义需要更多细节,包括长度、精度等,但这对我来说已经足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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