Skip to content
Advertisement

Postgres FOR loop syntax error at or near

I’m trying to write rows into an array from table_a and table_b and insert it into a table. This is my function def:

I’m getting an error in the for loop line stating “syntax error at or near FOR”. According to the official documentation, that is correct. What am I missing here?

Advertisement

Answer

You can only use a FOR loop in functions (or procedures) using language plpgsql. SQL has no for loops.

But you don’t need a loop or a function for this at all. This can be done using a simple INSERT statement with a SELECT statement as its source:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement