postgresql – Export each row of a table to a separate text file

I have a table in my database that contains data with three columns: I.D, first nameY notes. the notes The column contains unstructured text data that I want to export for each row as its own text file. This means that if I had 5 rows in my table, then I would export 5 .txt files. Here is my attempt so far:

do $$
to declare
arow record
varchar files;
start
for arow in
Select id, name, multi_text note
tie
files: = & # 39; / tmp / & # 39; || arow.id || & # 39; _ & # 39; || arow.name || & # 39; .txt & # 39 ;;
COPY (select the multiple text note where id = arow.id) TO files (FORMAT CSV);
final loop
finish;
$$;

Currently, there is a ERROR: syntax error in or near "files". I am using PostgreSQL in pgadmin. Any help is really appreciated!