我有一个包含记录类型和该类型变量的包:
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;
但我不知道如何获取有关给定记录的信息。是否有任何用于查询记录类型列的数据字典视图或内置函数?
谢谢。
PS: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
/
但是当我使用 %ROWTYPEs 时会出现问题,因为没有关于 %ROWTYPE 是什么的信息。所以我认为 PL/Scope 不是一个完整的解决方案......