【问题标题】:commit can not be excueted successfullycommit 无法成功执行
【发布时间】:2018-08-01 09:36:55
【问题描述】:

我有以下代码:该代码是从 demoimage 表映射到 fitheader 表。如果我注释掉提交,我可以运行代码而不会抱怨;线。但是 demoimage 表并没有改变,这让我在这些时间感到困惑。我在谷歌上搜索了很多关于这个问题的问题,但没有得到任何结果。

CREATE OR REPLACE FUNCTION public.linkjpeg(
)
RETURNS TABLE(matchtext text) 
LANGUAGE 'plpgsql'

COST 100
VOLATILE 
ROWS 1000
AS $BODY$

DECLARE
v_jpeghref demoImage.href%type;
v_fitsHREF fitsheader."HREF"%type;
v_uid varchar(500);
v_count bigint;
c_jpeg cursor for SELECT href from demoImage;
c_fitsHREF cursor for select t."HREF" from fitsheader t;
array_matches text[];
array_fitsHREF text[];
array_imgHref text[];
v_arrayDim bigint;
fileBone varchar(500);
fileBoneFits char(500);
c_checkUID cursor for select v_uid=any(array_matches);
c_checkfitsHREF cursor for select v_fitsHREF = any(array_fitsHREF);
c_matchrow cursor for select t.id_fitsheader from fitsheader t where t."HREF" like '%'||fileBoneFits||'%';
v_idfits bigint;
v_matchedFitsHREF fitsheader."HREF"%type;
i bigint;
_sql text;
BEGIN
  i := 0;

  open c_jpeg;
  loop
    fetch c_jpeg into v_jpegHref;

    v_uid := substring(v_jpegHref from '/member.uid.*');
      if(v_uid is not null) then

        array_imgHref := string_to_array(v_uid,'.');

        v_arrayDim := cardinality(array_imgHref);
        fileBone := array_to_string(array_imgHref[1:v_arrayDim-3],'.');
        -- replace xxxxx-x-xxxxxS. with null
        fileBoneFits := regexp_replace(filebone,'\d\d\d\d.\d.\d\d\d\d\d.S-','');

   open c_matchrow;
      begin
        fetch c_matchrow into v_idfits;
      exception 
        when others then
          raise notice '%','not found v_idfits';
        end;
        raise notice '%','v_idfits'||v_idfits;
        begin
          execute 'update demoImage set id_fitsheader='|| v_idfits ||' where href ='||quote_literal(v_jpegHref);
          **commit;**
          exception 
            when others then      
              raise notice '%','commit failed'||fileBoneFits;
          end;
        i := i+1;
        raise notice '%', i;
  close c_matchrow;
  end if;
  end loop;
  close c_jpeg;
END

问题是我永远无法成功执行提交。我可以单独在psql窗口中成功运行提交上面的sql。谁能帮我弄清楚我错在哪里?提前致谢!

【问题讨论】:

  • 你不能在函数中使用commitrollback

标签: postgresql plpgsql commit


【解决方案1】:

在 PL/pgSQL 函数中不能有 COMMITROLLBACK 这样的事务语句;该功能将在 v11 中提供。

【讨论】:

  • 感谢 Laurenz,那么我如何才能将更改提交到表格?
  • 调用该函数的客户端程序应该执行提交。要么让函数完成整个工作然后提交,要么让函数完成一大块工作并调用它几次,在两者之间提交。
猜你喜欢
  • 2018-12-03
  • 1970-01-01
  • 2020-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
  • 1970-01-01
  • 2017-12-02
相关资源
最近更新 更多