3

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
  • what were you planning to do with 'str', you haven't declared or used it anywhere except in the loop? Commented Oct 29, 2013 at 5:46

1 Answer 1

6

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; 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.