oracle – 是否有可能获得变量的最大可能长度
发布时间:2021-05-16 07:32:37 所属栏目:站长百科 来源:网络整理
导读:我想知道在plsql中是否存在给出变量最大长度的函数. 例如,如果我声明 DECLAREvaria VARCHAR2(7)BEGIN call of a function that would return 7END 即使varia为null,我也可以得到varchar的长度为7. —例如 create or replace TYPE ENREG_320_03 UNDER ENREG_3
谢谢大家 在您的特定用例中,由于您是在类型中而不是在匿名块或存储过程中执行此操作,因此您可以从user_type_attrs视图中获取信息:create or replace type t42 as object ( id number ) not final; / create or replace type t42_sub under t42 ( value varchar2(8),constructor function t42_sub(p_value in varchar2) return self as result,member function get_value return varchar2 ); / create or replace type body t42_sub as constructor function t42_sub(p_value in varchar2) return self as result is begin value := p_value; return; end t42_sub; member function get_value return varchar2 is l_attr_len number; begin select length into l_attr_len from user_type_attrs where type_name = 'T42_SUB' and attr_name = 'VALUE'; return case when self.value is null then lpad(' ',l_attr_len,' ') else rpad(self.value,' ') end; end get_value; end; / 然后使用该类型给出: with t as ( select t42_sub('AA').get_value() as val from dual union all select t42_sub(null).get_value() as val from dual ) select val,'<'|| val ||'>',length(val) from t; VAL '<'||VAL||'>' LENGTH(VAL) --------------- --------------- ----------- AA <AA > 8 < > 8 显然,您可以编写一个函数来获取类型/ attr_name的长度,而不是在每个成员函数中重复select. 我怀疑它会相当昂贵,除非你能想出一个缓存机制.如果对象是长寿的,你可以在我认为的构造函数中进行查找: create or replace type t42_sub under t42 ( value varchar2(8),max_value_len number,member function get_value return varchar2 ); / create or replace type body t42_sub as constructor function t42_sub(p_value in varchar2) return self as result is begin value := p_value; select length into max_value_len from user_type_attrs where type_name = 'T42_SUB' and attr_name = 'VALUE'; return; end t42_sub; member function get_value return varchar2 is begin return case when self.value is null then lpad(' ',max_value_len,' ') end; end get_value; end; / 但它似乎仍然是你应该在源代码控制而不是在运行时处理的东西,要么在类型声明中明确设置max_value_len:= 8(在值的旁边,所以你希望它们都需要注意它们都需要更改),或者使用替换变量的创建脚本. (编辑:淮安站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |