Postgresql函数忽略return语句并继续执行下一个return

Lui*_*ird 4 postgresql

我试图创建一个返回表的 postgres 函数,但是当我执行该函数时,return语句应该关闭或结束整个函数,但它一直忽略该结尾,然后继续。

该功能非常简单:只需验证字段是否为空。我知道如果我添加ELSE语句,问题就会得到解决,但是我不知道为什么它会忽略 RETURN ,我很想知道除了ELSE之外是否还有其他方法可以解决问题

create or replace function fnRegisterUserWin(rUsername text, rFname text, rLname text,rRole text, rBrand text) returns table(id_users int, message text, is_failure_location text, error_fields text[])
  language plpgsql
  as $$
  declare
    sanitazedUsername text;
    sanitazedFirstname text;
    sanitazedLastname text;
    sanitazedRole text;
    sanitazedBrand text;
    errorFields text;
  begin
    sanitazedUsername := str_clean(rUsername,true,true,true,true,true,true,true);
    sanitazedFirstname := str_clean(rFname,true,true,true,true,true,true,true);
    sanitazedLastname := str_clean(rLname,true,true,true,true,true,true,true);
    sanitazedRole := str_clean(rRole,true,true,true,true,true,true,true);
    sanitazedBrand := str_clean(rBrand,true,true,true,true,true,true,true);
    errorFields := '';

    if(empty2null(sanitazedUsername) is null OR empty2null(sanitazedFirstname) is null OR
       empty2null(sanitazedLastname) is null OR empty2null(sanitazedRole) is null OR
       empty2null(sanitazedBrand) is null) then
      if(empty2null(sanitazedUsername) is null) then  errorFields := errorFields || chr(39) || 'Username' || chr(39); end if;
      if(empty2null(sanitazedFirstname) is null) then errorFields := errorFields || ',' || chr(39) || 'Firstname' || chr(39); end if;
      if(empty2null(sanitazedLastname) is null) then errorFields := errorFields || ',' || chr(39) || 'Lastname' || chr(39); end if;
      if(empty2null(sanitazedRole) is null) then errorFields := errorFields || ',' || chr(39) || 'Role' || chr(39); end if;
      if(empty2null(sanitazedBrand) is null) then errorFields := errorFields || ',' || chr(39) || 'Brand' || chr(39); end if;
      return query select  0 as id_users, 'There are required fields that are empty, please complete them and try again. '::text as message,'Empty Fields'::text as is_failure_location,ARRAY[ltrim(',sa,aaa',',')]as errorFields;
    end if;
    return query execute 'select 0 as id_users, ' || chr(39) || 'There are required fields that are empty, please complete them and try again. ' || chr(39) || '::text as message,' || chr(39) || 'Empty Fields' || chr(39) || '::text as is_failure_location,ARRAY[' || ltrim(errorFields,',') ||']as errorFields';
  end;
  $$;

create function empty2null(text_i character varying)
  returns character varying
language plpgsql
as $$
declare
text_p varchar;
begin
if text_i = ''
then text_p := null;
else text_p := text_i;
end if;
return text_p;
end;
$$;

alter function empty2null(varchar)
  owner to postgres;
Run Code Online (Sandbox Code Playgroud)

这是返回:

0 必填字段为空,请填写并重试。空字段 {'Firstname'}

0 必填字段为空,请填写并重试。空字段{名字}

JGH*_*JGH 9

return query执行这两条语句是因为return query不退出函数。如果你想在块的末尾退出if,你可以添加一个return语句(单独)。

\n\n

根据文档

\n\n
\n

RETURN QUERY 实际上并不从函数 \xe2\x80\x94 返回,它们只是\n 将零行或多行附加到函数的结果集中。然后继续执行 PL/pgSQL 函数中的下一条语句。当执行连续的 RETURN NEXT 或 RETURN QUERY 命令时,就会建立结果集。最后的 RETURN 应该没有参数,\n 会导致控制退出函数(或者您可以让控制到达\n 函数的末尾)。

\n
\n