informix 里自定义函数返回 表 是怎么处理?
informix 里自定义函数返回 表 是怎么处理?
[解决办法]
可以返回表记录
CREATE FUNCTION atmv.tmp
()
returning varchar(20) as user_name,varchar(20) as user_cname;
BEGIN
define a,b varchar(20);
select user_name,user_cname from tb_user into temp tmp2;
foreach select user_name,user_cname into a,b
from tmp2
return a,b with resume;
end foreach;
drop table tmp2;
END;
END function;
[解决办法]
请参考:
以下函数字符串返回表集函数----
CREATE FUNCTION str_split
(v_str LVARCHAR(2000), v_split VARCHAR(20))
RETURNING int as ID , varchar(20) as strValue
define lv_str lvarchar(8000);
define lv_left varchar(200);
define lv_length int;
define lv_curIndex int;
define av_str lvarchar(8000);
define av_split varchar(20);
let av_str =ltrim(rtrim(v_str));
let av_split =v_split;
let lv_str =ltrim(rtrim(av_str));
let lv_length=0;
let lv_curIndex=0;
while instr(lv_str,av_split,1)<>-10000
let lv_length =lv_length+1;
let lv_left =substr(lv_str,1,instr(lv_str,av_split,1)-length(av_split));
--return lv_length,lv_left ;
return lv_length,lv_left with resume;
let lv_curIndex= lv_curIndex + length(lv_left)+length(av_split);
let lv_str=substr(lv_str,instr(lv_str,av_split,1)+length(av_split),length(lv_str));
end while;
if lv_curIndex<length(av_str) then
let lv_left=substr(av_str,lv_curIndex+length(av_split),length(av_str));
--v := row_type1(lv_length+1, lv_left);
-- pipe row (v);
return lv_length+1, lv_left;
end if
END FUNCTION;
调用方法:call str_split('a,b,c',',')
返回结果:
select调用方法:select * from table(str_split('a,b,c',','))
[解决办法]
请参考:
以下函数字符串返回表集函数----
CREATE FUNCTION str_split
(v_str LVARCHAR(2000), v_split VARCHAR(20))
RETURNING int as ID , varchar(20) as strValue
define lv_str lvarchar(8000);
define lv_left varchar(200);
define lv_length int;
define lv_curIndex int;
define av_str lvarchar(8000);
define av_split varchar(20);
let av_str =ltrim(rtrim(v_str));
let av_split =v_split;
let lv_str =ltrim(rtrim(av_str));
let lv_length=0;
let lv_curIndex=0;
while instr(lv_str,av_split,1)<>-10000
let lv_length =lv_length+1;
let lv_left =substr(lv_str,1,instr(lv_str,av_split,1)-length(av_split));
--return lv_length,lv_left ;
return lv_length,lv_left with resume;
let lv_curIndex= lv_curIndex + length(lv_left)+length(av_split);
let lv_str=substr(lv_str,instr(lv_str,av_split,1)+length(av_split),length(lv_str));
end while;
if lv_curIndex<length(av_str) then
let lv_left=substr(av_str,lv_curIndex+length(av_split),length(av_str));
--v := row_type1(lv_length+1, lv_left);
-- pipe row (v);
return lv_length+1, lv_left;
end if
END FUNCTION;
调用方法:call str_split('a,b,c',',')
返回结果:
select调用方法:select * from table(str_split('a,b,c',','))