I defined a function as following
CREATE OR REPLACE FUNCTION processActivityCommentTable() RETURNS INTEGER AS $$
DECLARE
activityCommentRow RECORD;
i RECORD;
activityId integer;
attachments text;
attachmentId integer;
cur1 CURSOR FOR SELECT ac.id, ac.attachments from activitycomment ac where ac.attachments is not null;
BEGIN
OPEN cur1;
FOR activityCommentRow in cur1
LOOP
RAISE NOTICE 'currently processing for %s ...', activityCommentRow.id;
-- can do some processing here
activityId = activityCommentRow.id;
attachments = activityCommentRow.attachments;
SELECT foo FROM regexp_split_to_table(attachments,E'{"id":') as foo;
FOR i in select * from foo
LOOP
select regexp_replace(i,'(,"name").*','') into attachmentId;
EXECUTE 'INSERT INTO attachment (activity_comment_id) values(' || attachmentId ||') where id= ' activityId;
END LOOP;
END LOOP;
CLOSE cur1;
END;
$$ LANGUAGE plpgsql;
while executing it
select processActivityCommentTable();
it gives me following errors
ERROR: cursor "cur1" already in use SQL state: 42P03 Context: PL/pgSQL function processactivitycommenttable() line 12 at FOR over cursor
Can anyone please help?