Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

plsql postgresql : SQL Error [42601]: RETURN QUERY

This code below dispalys this error

SQL Error [42601]: ERREUR: erreur de syntaxe à la fin de l'entrée
  Où : fonction PL/pgSQL users_tables(), ligne 26 à RETURN QUERY

I do not understand why !
In fact i launched the query inside the loop and it works fine
I do not see any logical error in my code

DROP FUNCTION users_tables();

create or replace
function users_tables()
 returns table (table_name text, id int)
 language plpgsql
as $function$
declare 
   _table_name text;
   _column_name text;
begin
    for _table_name,_column_name in 
select
            distinct t.table_name,
    c2.column_name
from
            information_schema.tables t
inner join information_schema.columns c1 on
            c1.table_name = t.table_name
    and c1.table_schema = t.table_schema
    and c1.column_name = 'utilisateur_fk'
inner join information_schema.columns c2 on
            c2.table_name = t.table_name
    and c2.table_schema = t.table_schema
    and lower(c2.column_name) like 'date%'
order by 
            t.table_name,
    c2.column_name
limit 
            2
    loop
             return QUERY execute
        'SELECT ' || _column_name || ',id FROM  ' _table_name;
end loop;
end

$function$
;

select users_tables();

Could you please help me ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you probably should write :
'SELECT ' || _column_name || ',id FROM ' || _table_name;
with || before _table_name.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading