Home > database >  plsql postgresql : SQL Error [42601]: RETURN QUERY
plsql postgresql : SQL Error [42601]: RETURN QUERY

Time:12-16

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 ?

CodePudding user response:

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

  • Related