问题描述
出于某种原因,我在表中有某些字段,它们是字符集合。不同长度的字符。示例:
For some reason, I have certain fields in a table, that are collections of chars. Chars of different length. Example:
create or replace type t_charray_1 as varray(5) of char(1);
create or replace type t_charray_2 as varray(5) of char(2);
create or replace type t_charray_3 as varray(5) of char(3);
create or replace type t_charray_4 as varray(5) of char(4);
create table mytable (
field1 number,
field2 t_charray_1,
field3 t_charray_3,
另外,我有一个返回 mytable
记录的(固定长度)字符串表示形式的函数,该函数调用其他函数例如:
Also, I have a function that returns a (fixed length) string representation of a mytable
record. This function calls other functions that are returning the string representation of a given collection-typed field. Examples:
function to_chr(
p_array in t_charray_1,
pad_length in number,
p_list_length in number
) return char as
v_res varchar2(255) := '';
begin
for i in 1 .. p_list_length loop
if p_array is not null and p_array.exists(i) and p_array(i) is not null then
v_res := v_res || rpad(p_array(i), pad_length, ' ');
else
v_res := v_res || rpad(' ', pad_length, ' ');
end if;
end loop;
return v_res;
end to_chr;
------------------------------------------------------------------------------
function to_chr(
p_array in t_charray_2,
pad_length in number,
p_list_length in number
) return char as
v_res varchar2(255) := '';
begin
for i in 1 .. p_list_length loop
if p_array is not null and p_array.exists(i) and p_array(i) is not null then
v_res := v_res || rpad(p_array(i), pad_length, ' ');
else
v_res := v_res || rpad(' ', pad_length, ' ');
end if;
end loop;
return v_res;
end to_chr;
请注意,这些函数是相互重载的版本。他们签名的唯一区别是 p_array
参数的类型。
Note that these functions are overloaded versions of each other. The only difference in their signature is the type of the p_array
argument.
请注意 the这些功能的主体是相同的。
Please also note that the bodies of these functions are identical.
动机
I想要消除重复的代码。我的选择是什么?
I want to eliminate duplicate code. What are my choices?
编辑我听说过sys.anydata,但从未使用它。试试看:
EDIT I have heard of sys.anydata but never used it. Can it be a solution?
解决方案
推荐答案
> 创建或替换函数to_chr(p_array in anydata,
pad_length in number,
p_list_length in number)return char as
v_res varchar2(255): ='';
x数字;
v_array t_charray_4;
v_array1 t_charray_1;
v_array2 t_charray_2;
v_array3 t_charray_3;
begin
dbms_output.put_line(p_array.GetTypeName);
case p_array.GetTypeName
当'< schema> .T_CHARRAY_1'时,则
x:= p_array.GetCollection(v_array1);
从dual中选择cast(v_array1作为t_charray_4)到v_array中;
当'< schema> .T_CHARRAY_2'时,则
x:= p_array.GetCollection(v_array2);
从dual中选择cast(v_array2作为t_charray_4)到v_array中;
当'< schema> .T_CHARRAY_3'时,则
x:= p_array.GetCollection(v_array3);
从dual中选择cast(v_array3作为t_charray_4)转换为v_array;
当'< schema> .T_CHARRAY_4'时,则
x:= p_array.GetCollection(v_array);
最终案例;
for i .. 1 .. p_list_length循环
如果v_array不为空且v_array.exists(i)和v_array(i)不为null,则
v_res:= v_res | | rpad(v_array(i),pad_length,'');
else
v_res:= v_res || rpad('',pad_length,'');
结束if;
结束循环;
返回v_res;
结束to_chr;
您可以像这样运行它:
declare
p_array anydata;
v_array t_charray_3 := new t_charray_3('aaa', 'bbb');
v_res varchar2(255);
begin
p_array := anydata.convertcollection(v_array);
v_res := to_chr(p_array => p_array, pad_length => 2, p_list_length => 3);
end;
这篇关于Oracle PL / SQL中的动态类型或泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!