记录类型反射/填充未知类型的记录变量(PL / SQL)

时间:2013-07-29 13:38:26

标签: oracle plsql

我有一个包含记录类型和该类型变量的包:

CREATE PACKAGE my_package AS
  TYPE t_rec IS RECORD(
    id           NUMBER,
    name         VARCHAR2(100),
    last_updated DATE
  );
  v_var t_rec;
END;

我想创建一个用于填充任何记录变量的通用过程,因此可以像这样调用它:

BEGIN
  populate_record('my_package.v_var', 'MY_TABLE', CHARTOROWID(:VAR1));
END;

该程序的逻辑可能如下所示:

PROCEDURE populate_record(
  p_var_name   IN VARCHAR2,
  p_table_name IN VARCHAR2,
  p_rowid      IN ROWID
) IS
BEGIN
  -- determining a type of a given record variable
  -- determining names and data types of given record variable's columns
  -- selecting a row from a given table by a given rowid
  -- filling out a given record variable by mapping table columns
  --   to record columns by their names (using dynamic PL/SQL)
END;

但我不知道如何获取有关给定记录的信息。是否有用于查询记录类型列的数据字典视图或内置函数?
感谢。

P.S。:11.2

已更新

我找到了一种使用PL / Scope获取有关记录的信息的方法:

CREATE OR REPLACE VIEW user_record_types AS
SELECT
  i.object_name package_name,
  i.name type_name
FROM
  user_identifiers i
WHERE
  i.object_type = 'PACKAGE'
  AND i.type = 'RECORD'
  AND i.usage = 'DECLARATION'
/

CREATE OR REPLACE VIEW user_record_types_columns AS
SELECT
  i1.object_name package_name,
  i1.name type_name,
  ROW_NUMBER() OVER (
    PARTITION BY
      i1.object_name,
      i1.name
    ORDER BY
      i2.usage_id
  ) column_num,
  i2.name column_name,
  i3.name column_type,
  i3.type column_type_class
FROM
  user_identifiers i1,
  user_identifiers i2,
  user_identifiers i3
WHERE
  i1.object_type = 'PACKAGE'
  AND i1.type = 'RECORD'
  AND i1.usage = 'DECLARATION'
  AND i2.object_name = i1.object_name
  AND i2.object_type = i1.object_type
  AND i2.type = 'VARIABLE'
  AND i2.usage = 'DECLARATION'
  AND i2.usage_context_id = i1.usage_id
  AND i3.object_name = i2.object_name
  AND i3.object_type = i2.object_type
  AND i3.usage = 'REFERENCE'
  AND i3.usage_context_id = i2.usage_id
/

CREATE OR REPLACE VIEW user_record_variables AS
SELECT
  i1.object_name package_name,
  i1.name variable_name,
  CASE i2.type
    WHEN 'RECORD' THEN
      i2.object_name
    WHEN 'PACKAGE' THEN
      i2.name
  END type_package_name,
  CASE i2.type
    WHEN 'RECORD' THEN
      i2.name
    WHEN 'PACKAGE' THEN
      i3.name
  END type_name
FROM
  user_identifiers i1,
  user_identifiers i2,
  user_identifiers i3
WHERE
  i1.object_type = 'PACKAGE'
  AND i1.type = 'VARIABLE'
  AND i1.usage = 'DECLARATION'
  AND i2.object_name = i1.object_name
  AND i2.object_type = i1.object_type
  AND i2.type IN ('RECORD', 'PACKAGE')
  AND i2.usage = 'REFERENCE'
  AND i2.usage_context_id = i1.usage_id
  AND i3.object_name (+) = i2.object_name
  AND i3.object_type (+) = i2.object_type
  AND i3.type (+) = 'RECORD'
  AND i3.usage (+) = 'REFERENCE'
  AND i3.usage_context_id (+) = i2.usage_id
/

但是当我使用%ROWTYPE时会出现问题,因为没有关于%ROWTYPE的“含义”的信息。所以我认为PL / Scope不是一个完整的解决方案......

1 个答案:

答案 0 :(得分:1)

如果您有访问权限,则可以尝试使用表格user_identifiersall_identifiers,如this other SO帖子所示。

您可能需要重新编译软件包:

alter package  my_types compile plscope_settings='IDENTIFIERS:ALL' reuse settings;

不幸的是,这只能从11gR1获得。