如何查询有关PL / SQL记录类型的元数据

时间:2018-08-03 13:30:22

标签: oracle plsql

如果我的记录类型如下:

create or replace PACKAGE ITEM_MANAGEMENT AUTHID CURRENT_USER
as
    type item IS RECORD (
        id                         NUMBER,
        Description                VARCHAR2(30),
        Category                   VARCHAR2(30)
    );
END ITEM_MANAGEMENT;

从PL / SQL中,我可以声明l_item ITEM_MANAGEMENT.Item;类型的记录变量

我希望能够在Oracle数据库中查询该记录类型的属性及其类型。

例如,如果要对表执行此操作,请执行以下操作:

select COLUMN_NAME, DATA_TYPE, DATA_LENGTH
from ALL_TAB_COLUMNS
where 1 = 1
AND OWNER = 'OWNER'
AND TABLE_NAME = 'TABLENAME';

如何查询有关PL / SQL记录类型的元数据?

1 个答案:

答案 0 :(得分:2)

您似乎正在谈论的是PL / SQL记录类型。仅当编译存储的PL / SQL以收集PLSCOPE标识符时,才可以检查PL / SQL记录类型(否则,查询将不会获得任何数据)。

我将在此处发布一个示例。但是首先让我说,对于任何生产代码,我都不必这样做。根据我的经验,您尝试做的事情不是“正常的”,也许您需要重新评估您的方法。无论如何,就在这里。

-- First, tell your session that all compilations should collect
-- PLSCOPE identifiers.  If you neglect to do this, you can always ALTER 
-- a package later to recompile it with PLSCOPE identifiers as follows:
--     alter package mypackage compile 
--         plscope_settings='IDENTIFIERS:ALL' reuse settings;
ALTER SESSION SET plscope_settings='IDENTIFIERS:ALL';

CREATE OR REPLACE PACKAGE so_test1 AS

 TYPE Item IS RECORD (
    id                         NUMBER,
    Description                VARCHAR2(30),
    Category                   VARCHAR2(30)
);

END so_test1;


-- Now, the information you want will be in DBA_IDENTIFIERS, which is 
-- stored hierarchically.  This query will print that hierarchy, starting
-- with the definition of your "ITEM" record type.     
WITH v AS (
  SELECT    Line,
            Col,
            NAME,
            TYPE,
            USAGE,
            USAGE_ID,
            USAGE_CONTEXT_ID
    FROM USER_IDENTIFIERS
      WHERE Object_Name = 'SO_TEST1'
        AND Object_Type = 'PACKAGE'
)
SELECT LPAD(' ', 2*(Level-1)) || NAME,
TYPE, USAGE, USAGE_CONTEXT_ID
  FROM v
  START WITH name = 'ITEM' and type = 'RECORD' and usage = 'DECLARATION'
  CONNECT BY PRIOR USAGE_ID = USAGE_CONTEXT_ID
  ORDER SIBLINGS BY Line, Col
/

示例输出:

+---------------+--------------------+-------------+------------------+
|     NAME      |        TYPE        |    USAGE    | USAGE_CONTEXT_ID |
+---------------+--------------------+-------------+------------------+
| ITEM          | RECORD             | DECLARATION |                1 |
|   ID          | VARIABLE           | DECLARATION |                2 |
|     NUMBER    | NUMBER DATATYPE    | REFERENCE   |                3 |
|   DESCRIPTION | VARIABLE           | DECLARATION |                2 |
|     VARCHAR2  | CHARACTER DATATYPE | REFERENCE   |                5 |
|   CATEGORY    | VARIABLE           | DECLARATION |                2 |
|     VARCHAR2  | CHARACTER DATATYPE | REFERENCE   |                7 |
+---------------+--------------------+-------------+------------------+