Sorry I had to edit this to account for new information. However what I am trying to do is the same as before. I am trying to basically loop through specific schemas within the same database and access a Table within each schema that every schema has in common (called "Edit"). Then, I want to display the schema name alongside one row of the Edit Table. However, I keep getting an error saying "syntax error at or near cursor"(this code - CURSOR cur_edtf IS t_query;) I am assuming this means that something is fundamentally wrong with my syntax, but I am very new to Postgres Dynamic SQL and not the best with SQL so I am not totally sure what I am doing wrong... can anyone offer suggestions?
DECLARE
CURSOR cur_project IS SELECT * from data."Project" where "Project"."Complete" = True and "Project"."StatusID"=1;
proj_entity cur_project%ROWTYPE;
t_query text;
BEGIN
OPEN cur_project;
LOOP
fetch cur_project into proj_entity;
EXIT WHEN cur_project%NOTFOUND;
t_query := 'SELECT * from ' || proj_entity."SchemaName" || '."Edit" order by ' || proj_entity."SchemaName" || '."Edit"."Version" desc limit 1;';
CURSOR cur_edtf IS t_query;
edtf_entity cur_edtf%ROWTYPE;
OPEN cur_edtf;
LOOP
fetch cur_edtf into edtf_entity;
EXIT WHEN cur_edtf%NOTFOUND;
dbms_output.put_line(proj_entity."SchemaName", edtf_entity);
END LOOP;
CLOSE cur_edtf;
END LOOP;
CLOSE cur_project;
END;