问题
Here I am trying to concatenate a string with ,:
CREATE FUNCTION looping() RETURNS TABLE(round text)
DECLARE
i RECORD;
BEGIN
FOR i IN select regexp_split_to_table('33,55,66,88', ',') as "asd"
LOOP
str:= str || ',' ||(select i."asd");
END LOOP;
END;
$$ LANGUAGE plpgsql;
Is it true or am I missing something?
回答1:
Often, a set-based operation with standard SQL functions is superior to looping.
But if you need the control structure in plpgsql, would work like this (one of many ways):
CREATE FUNCTION f_loop2(OUT str text)
RETURNS text AS
$func$
DECLARE
i text;
BEGIN
str := '';
FOR i IN
SELECT regexp_split_to_table('33,55,66,88', ',')
LOOP
str := str || ',' || i;
END LOOP;
END
$func$ LANGUAGE plpgsql;
来源:https://stackoverflow.com/questions/19650581/for-loops-with-string-in-postgresql