【问题标题】:postgress error handling from function来自函数的 postgres 错误处理
【发布时间】:2014-07-01 18:21:42
【问题描述】:

我尝试运行运行其他函数并捕获错误的函数,以防其中一个或几个函数失败。 函数是这样的(我删除了不必要的部分):

CREATE OR REPLACE FUNCTION func()
 RETURNS void
 LANGUAGE plpgsql
AS $function$

BEGIN
cur_time:=now();
    FOR r IN select func_num FROM mytable order by func_num
    LOOP

        select schema , function_name, last_run, period into _schema, _func_name,_last_run, _period  from mytable
       ;

        if _last_run+_period <=cur_time then perform _schema||'.'||_func_name||'()' ; end if;


        EXCEPTION WHEN OTHERS THEN

            RAISE NOTICE 'exception (code=%): %', SQLCODE, SQLERRM;           
            _error:=SQLCODE; 

            if _last_run+_period >=cur_time then update mytable set error=_error where schema=_schema and function_name=_func_name; end if;

    END LOOP;

    RETURN;

END;

我收到以下错误:“ERROR: ERROR: syntax error at or near "EXCEPTION"”,我找不到它:((

将不胜感激!

【问题讨论】:

    标签: postgresql error-handling


    【解决方案1】:

    如果您阅读relevant part of the manuals,您会发现您需要 BEGIN ... EXCEPTION

    ...
    LOOP
      BEGIN
      ...
      EXCEPTION
      ...
      END;
    END LOOP;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-11
      • 2017-05-16
      • 1970-01-01
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      相关资源
      最近更新 更多