postgresql 控件到达功能结束,但未返回

8ehkhllq  于 11个月前  发布在  PostgreSQL
关注(0)|答案(2)|浏览(129)

我在PostgreSQL中有这个函数,当我尝试执行它时,我收到这个错误消息:
错误:控件到达函数末尾时没有返回
PL/pgSQL函数“SA_PRJ”.usp_add_timesheet_record_neww(整数,整数,数字,数字,字符变化,字符变化)
我不确定但我想回报是个问题,最后一定有回报吧?

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;

字符串
谢谢.

mwecs4sa

mwecs4sa1#

将函数定义为:

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
...

字符串
或者你使用RETURN而不是RAISE NOTICE(如果你想返回的话)

dgenwo3n

dgenwo3n2#

我创建了PL/pgSQL function,它对RETURNS INTEGER不返回任何内容,如下所示:

CREATE FUNCTION my_func() RETURNS INTEGER
AS $$                     -- ↑ Here ↑
BEGIN
END;
$$ LANGUAGE plpgsql;

字符串
然后,调用my_func()得到相同的错误,如下所示:

postgres=# SELECT my_func();
ERROR:  control reached end of function without RETURN
CONTEXT:  PL/pgSQL function my_func()


因此,我将RETURN 2;设置为my_func(),如下所示:

CREATE FUNCTION my_func() RETURNS INTEGER
AS $$
BEGIN
  RETURN 2; -- Here
END;
$$ LANGUAGE plpgsql;


然后,我可以调用my_func()而不会出错,如下所示:

postgres=# SELECT my_func();
 my_func
---------
       2
(1 row)


或者,我将RETURNS INTEGER替换为RETURNS VOID,如下所示:

CREATE FUNCTION my_func() RETURNS VOID
AS $$                     -- ↑ Here ↑
BEGIN
END;
$$ LANGUAGE plpgsql;


然后,我可以调用my_func()而不会出错,如下所示:

postgres=# SELECT my_func();
 my_func
---------

(1 row)

相关问题