csa*_*dam 9 oracle reflection variables plsql dump
PL/SQL中是否有一个函数来显示变量的确切类型,比如SQL中的DUMP函数?
我尝试了以下内容
DECLARE
l_variable INTEGER := 1;
BEGIN
DBMS_OUTPUT.PUT_LINE (DUMP (l_variable));
END;
Run Code Online (Sandbox Code Playgroud)
但它给出了以下错误:
PLS-00204:函数或伪列'DUMP'只能在SQL语句中使用
您可以使用PL/Scope创建此功能.但它不适用于匿名块,您需要将该变量作为字符串引用.
create or replace function get_plsql_type_name
(
p_object_name varchar2,
p_name varchar2
) return varchar2 is
v_type_name varchar2(4000);
begin
select reference.name into v_type_name
from user_identifiers declaration
join user_identifiers reference
on declaration.usage_id = reference.usage_context_id
and declaration.object_name = reference.object_name
where
declaration.object_name = p_object_name
and declaration.usage = 'DECLARATION'
and reference.usage = 'REFERENCE'
and declaration.name = p_name;
return v_type_name;
end;
/
Run Code Online (Sandbox Code Playgroud)
例:
alter session set plscope_settings = 'IDENTIFIERS:ALL';
create or replace type my_weird_type is object
(
a number
);
create or replace procedure test_procedure is
var1 number;
var2 integer;
var3 my_weird_type;
subtype my_subtype is pls_integer range 42 .. 43;
var4 my_subtype;
begin
dbms_output.put_line(get_plsql_type_name('TEST_PROCEDURE', 'VAR1'));
dbms_output.put_line(get_plsql_type_name('TEST_PROCEDURE', 'VAR2'));
dbms_output.put_line(get_plsql_type_name('TEST_PROCEDURE', 'VAR3'));
dbms_output.put_line(get_plsql_type_name('TEST_PROCEDURE', 'VAR4'));
end;
/
begin
test_procedure;
end;
/
NUMBER
INTEGER
MY_WEIRD_TYPE
MY_SUBTYPE
Run Code Online (Sandbox Code Playgroud)