skip to Main Content

I have a question, what is missing or wrong in the RETURN statement that it keeps crashing error 2F005

I am creating a table to log errors

    drop table if exists public.funkcja_x;
    create table public.funkcja_x 
    (
        error_alert text
    );

I am creating function

drop function if exists public.test();
create or replace function public.test()
returns text as
    $body$
 
        declare
            v_error text;
        begin

        -- i intentionally create a table from a table that doesn't exist to force an error

            drop table if exists public.tabela_final;
            create table public.tabela_final as
            select * from public.tabela_posrednia;          
 
        return 'OK';
        exception when others then 
            v_error := SQLERRM;
            insert into public.funkcja_x (error_alert) values (v_error);
        end;
    $body$
language plpgsql volatile cost 100;
alter function public.test() owner to postgres;

evokes:

select public.test();

and gets error:

BŁĄD: osiągnięto koniec funkcji, brakuje instrukcji RETURN
Stan SQL: 2F005
Kontekst: funkcja PL/pgSQL test()

2

Answers


  1. Chosen as BEST ANSWER

    Hmmm I found a solution, you have to replace TEXT to VOID and then it works :) but thank you for your interest and sorry for the spam :)


  2. You are missing RETURN statement. Any non void function in PL/pgSQL should to return data. Your function uses RETURNS text but returns nothing. Probably miss RETURN 'false'.

    You can use boolean instead text as return type.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search