我有以下字符串“3,45,543,6,89”。需要像 table 这样的完整函数输出。请帮助我通过 postgresql 函数获取输出

I have the following string '3,45,543,6,89'. Need output like table thorugh function. Pl help me get output through postgresql function

CREATE OR REPLACE FUNCTION public.SplitToTable(
iv_datalist  varchar,iv_Separator varchar)
    RETURNS TABLE(out_param varchar)  
    LANGUAGE 'plpgsql'

    COST 100
    VOLATILE 
    ROWS 1000
AS $BODY$
BEGIN

RETURN query select (select regexp_split_to_table(iv_datalist, iv_Separator) as out_param );

END
$BODY$;

当我运行

select * from splitToTable('3,34,4,545,35,3',',');

我收到这个错误:

ERROR:  structure of query does not match function result type
DETAIL:  Returned type text does not match expected type character varying in column 1.
CONTEXT:  PL/pgSQL function splittotable1(character varying,character varying) line 4 at RETURN QUERY
SQL state: 42804

您必须将函数声明为 RETURNS text 而不是 RETURNS varchar。在任何地方都使用 text,因为这是 PostgreSQL 中首选的字符串类型。这样,类型转换就没有问题了。

函数可以写得更简单

CREATE OR REPLACE FUNCTION public.SplitToTable(
   iv_datalist text,
   iv_Separator text
) RETURNS TABLE(out_param text)  
    LANGUAGE sql
    IMMUTABLE
AS
$BODY$SELECT * FROM regexp_split_to_table(iv_datalist, iv_Separator);$BODY$;

本质上,这个功能是没用的;你可以直接使用 regexp_split_to_table