sqldatabasepostgresqlpostgresql-9.1

Control reached end of function without RETURN


I have this function in PostgreSQL and when I'm trying to execute it, I get this error message:

ERROR: control reached end of function without RETURN
CONTEXT: PL/pgSQL function "SA_PRJ".usp_add_timesheet_record_neww(integer,integer,numeric,numeric,character varying,character varying)

I'm not sure but I think the return is the problem, must have return at the end?

CREATE OR REPLACE FUNCTION "SA_PRJ".usp_add_timesheet_record_neww(p_uid integer, p_project_id integer, p_allocated_time numeric, p_achieved_time numeric, p_task_desc character varying, p_obs character varying)
  RETURNS character varying AS
$BODY$
declare alloc_id integer;
declare project integer;
declare allocated integer;
declare allocated_time numeric;
BEGIN

    project := p_project_id;

    allocated_time := (SELECT SUM(fld_allocated_time)
    FROM "SD_PRJ".tbl_project_timesheet
    WHERE fld_project_id = project);

    allocated := (SELECT fld_allocated_days FROM "SD_PRJ".tbl_project where fld_id = project);

    if not "SA_ADM".usp_check_permission(p_uid, 'SA_PRJ', 'usp_add_timesheet_record') then
    raise exception 'User ID % nu are permisii pentru aceasta operatie!', p_uid;
    end if;

    select fld_id into alloc_id from "SD_PRJ".tbl_project_allocation where fld_emp_id = p_uid and fld_project_id = p_project_id;

    BEGIN
    IF (allocated > allocated_time) THEN

    INSERT INTO "SD_PRJ".tbl_project_timesheet(fld_emp_id, fld_project_id, fld_is_allocated,fld_allocated_time, fld_achieved_time, fld_task_desc, fld_obs)
    VALUES (p_uid,p_project_id,coalesce(alloc_id,0), p_allocated_time, p_achieved_time,p_task_desc, p_obs);
    RAISE NOTICE 'OK';

    ELSE
        RAISE NOTICE 'Not OK!';
    END IF;
    END;
END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

Thanks.


Solution

  • Either define the function as:

    CREATE OR REPLACE FUNCTION "SA_PRJ".usp_add_timesheet_record_neww(p_uid integer, p_project_id integer, p_allocated_time numeric, p_achieved_time numeric, p_task_desc character varying, p_obs character varying)
    RETURNS void AS
    ...
    

    or you use RETURN instead of RAISE NOTICE (if thats what you want to return)