I haven't use the RETURNING clause before, but the "INTO" option, at least in SELECT sentences, must be outside of the string expression. This way: EXECUTE 'some query' INTO variable;
EXECUTE 'INSERT INTO '||tablename||' ('||fields||') VALUES ('||vals||') RETURNING currval('''||seqname||''') INTO newid'
and I get the response:
ERROR: syntax error at or near "INTO" LINE 1: ...','2008','4',NULL) RETURNING currval('id_seq') INTO newid
And I do not understand this error. If I take the INSERT command and run it by hand, it works fine, but it doesn't work in the function when called by execute. Anybody has an idea on what is wrong and what to do about it?