gpt4 book ai didi

oracle - PL/SQL中如何查看变量的类型?

转载 作者:行者123 更新时间:2023-12-02 02:47:19 25 4
gpt4 key购买 nike

PL/SQL 中是否有函数可以显示变量的确切类型,如 SQL 中的 DUMP 函数?

我尝试过以下方法

DECLARE
l_variable INTEGER := 1;
BEGIN
DBMS_OUTPUT.PUT_LINE (DUMP (l_variable));
END;

但它给出了以下错误:

PLS-00204: function or pseudo-column 'DUMP' may be used inside a SQL statement only

最佳答案

您可以使用 PL/Scope 创建此函数。但它不适用于匿名 block ,并且您需要将变量作为字符串引用。

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;
/

示例:

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

关于oracle - PL/SQL中如何查看变量的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17134293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com