如何在 pgsql 9.1 中执行返回空值的过程?

bma*_*ies 8 postgresql

如果我使用 pgsql 命令在文件中执行以下内容,它会抱怨需要使用“perform”调用该过程。但是当我尝试使用 perform 时,它告诉我未定义 perform 。解决办法是什么?

create or replace function waitris() returns void as 
$$
declare
    cnt integer;
begin
    loop
        select count(*) into cnt from taskflag where taskid = 'rdfdb' and state != 2;
        if cnt = 0 then   
            exit;
        end if;
        select pg_sleep(1);
    end loop;
end;
$$ 
language plpgsql;

select waitris();
Run Code Online (Sandbox Code Playgroud)

dez*_*zso 9

你如何调用你的函数没有问题。当且仅当cnt大于零时,您得到的错误来自执行它。你得到的错误是

ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function "waitris" line 9 at SQL statement
Run Code Online (Sandbox Code Playgroud)

并且明确指出问题出SELECT pg_sleep(1);在线路上。这是你应该更换的地方SELECTPERFORM

否则,如果没有CONTINUE. 即使你添加了它,循环的行为也很难遵循。你最好使用WHILE循环代替。还有一个:如您所见,我将变量名称更改countcnt以提高可读性。