For loops with string in PostgreSQL

与世无争的帅哥 提交于 2019-12-11 06:42:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!