如何在plpgsql函数中获取当前行值

how to get current row value in plpgsql function

我需要创建 plpgsql 方法,它使用当前行值而不在更新命令中传递参数。

我试过了

create temp table test ( test text, result text ) on commit drop;
insert into test values ('1','');

CREATE OR REPLACE FUNCTION public.gettest() RETURNS text AS $$
DECLARE
  comp text := NULL;

BEGIN
EXECUTE 'SELECT ''Current row is '' ||test.test' INTO comp;
RETURN comp;
END; $$ LANGUAGE plpgsql STRICT STABLE;

update test set result = 'Result: ' || gettest();

但出现异常

ERROR:  missing FROM-clause entry for table "test"
LINE 1: SELECT 'Current row is ' ||test.test
                                   ^
QUERY:  SELECT 'Current row is ' ||test.test
CONTEXT:  PL/pgSQL function gettest() line 6 at EXECUTE statement
********** Error **********

ERROR: missing FROM-clause entry for table "test"
SQL state: 42P01
Context: PL/pgSQL function gettest() line 6 at EXECUTE statement

如何解决? 如何在不将 vaue 传递给 plpgsql 方法参数的情况下进行修复?

没有 "implicit current row" 这样的东西。您 可以将需要的函数作为参数传递给函数。但是,如果您愿意,可以传递完整的行:

create temp table test (val text, result text ) on commit drop;
insert into test values ('1','');
insert into test values ('2','');

CREATE OR REPLACE FUNCTION gettest(p_test test) RETURNS text AS $$
DECLARE
  comp text := NULL;
BEGIN
   comp := 'Current row is '||p_test.val;
RETURN comp;
END; $$ LANGUAGE plpgsql STRICT STABLE;

update test set result = 'Result: ' || gettest(test);

我必须将列 test 重命名为其他名称,否则调用 gettest(test) 将引用 而不是整个 table (=row) 因此它不起作用。