I want to do something like this:
CREATE OR REPLACE FUNCTION ff(int, text) RETRUNS integer AS $$
DECLARE
r text;
BEGIN
FOR r IN SELECT string_to_array($2, ',')
LOOP
INSERT INTO test(i, t) VALUES($1, r);
END LOOP;
RETRUN 0;
END
$$LANGUAGE pgsql;
I hope that the function SELECT ff(3, 'a,b'); does
INSERT INTO test(i, t) VALUES(3, 'a');
INSERT INTO test(i, t) VALUES(3, 'b');
You don’t need a loop for that, you could use
unnestto convert the array fromstring_to_arrayinto a set of rows and then use a simpleinsert ... selectconstruct:I’ve also corrected some typos (
RETRUNS,RETRUN, andpgsql) along the way.You could also use
regexp_split_to_table:If you’re stuck in the 8.1 stone age and can’t do anything about it, then perhaps something like this would work:
I think that should work in 8.1 but I don’t have access to 8.1 to check.