由于没有结果目的地,为什么这个函数不能 return 一个整数?
Why is this function not able to return an integer due to no destination for result?
我有一个简单的函数需要 return 来自 select 语句的整数。就是这样:
CREATE OR REPLACE FUNCTION validate(_identityid integer, _postid integer) RETURNS integer
LANGUAGE plpgsql
AS
$$
BEGIN
SELECT postid
FROM post
WHERE
postid = _postid
AND
identityid = _identityid;
END;
$$;
如果我运行上面的函数我得到以下错误:
[42601] ERROR: query has no destination for
result data Hint: If you want to discard the
results of a SELECT, use PERFORM instead.
为什么结果数据没有目的地?为什么不只是 return SELECT
语句?
As documented in the manual查询结果需要存储在某处。
CREATE OR REPLACE FUNCTION validate(_identityid integer, _postid integer) RETURNS integer
LANGUAGE plpgsql
AS
$$
DECLARE
l_result integer;
BEGIN
SELECT postid
into l_result
FROM post
WHERE
postid = _postid
AND
identityid = _identityid;
return l_result;
END;
$$;
但是要像那样包装一个简单的 SELECT,无论如何使用 language sql
函数要好得多,您可以在那里直接 return 结果。
CREATE OR REPLACE FUNCTION validate(_identityid integer, _postid integer)
RETURNS integer
LANGUAGE sql
stable
AS
$$
SELECT postid
FROM post
WHERE
postid = _postid
AND
identityid = _identityid;
$$;
如果您想使用 plpgsql
而不使用 RETURN
来执行此操作,您需要一个 OUT
参数:
CREATE OR REPLACE FUNCTION public.out_test(in_val integer, OUT out_val integer)
LANGUAGE plpgsql
AS $function$
BEGIN
SELECT INTO out_val in_val + 1;
END;
$function$
select out_test(3);
out_test
----------
4
根据文档 Returning:
The return value of a function cannot be left undefined. If control reaches the end of the top-level block of the function without hitting a RETURN statement, a run-time error will occur. This restriction does not apply to functions with output parameters and functions returning void, however. In those cases a RETURN statement is automatically executed if the top-level block finishes.
我有一个简单的函数需要 return 来自 select 语句的整数。就是这样:
CREATE OR REPLACE FUNCTION validate(_identityid integer, _postid integer) RETURNS integer
LANGUAGE plpgsql
AS
$$
BEGIN
SELECT postid
FROM post
WHERE
postid = _postid
AND
identityid = _identityid;
END;
$$;
如果我运行上面的函数我得到以下错误:
[42601] ERROR: query has no destination for result data Hint: If you want to discard the results of a SELECT, use PERFORM instead.
为什么结果数据没有目的地?为什么不只是 return SELECT
语句?
As documented in the manual查询结果需要存储在某处。
CREATE OR REPLACE FUNCTION validate(_identityid integer, _postid integer) RETURNS integer
LANGUAGE plpgsql
AS
$$
DECLARE
l_result integer;
BEGIN
SELECT postid
into l_result
FROM post
WHERE
postid = _postid
AND
identityid = _identityid;
return l_result;
END;
$$;
但是要像那样包装一个简单的 SELECT,无论如何使用 language sql
函数要好得多,您可以在那里直接 return 结果。
CREATE OR REPLACE FUNCTION validate(_identityid integer, _postid integer)
RETURNS integer
LANGUAGE sql
stable
AS
$$
SELECT postid
FROM post
WHERE
postid = _postid
AND
identityid = _identityid;
$$;
如果您想使用 plpgsql
而不使用 RETURN
来执行此操作,您需要一个 OUT
参数:
CREATE OR REPLACE FUNCTION public.out_test(in_val integer, OUT out_val integer)
LANGUAGE plpgsql
AS $function$
BEGIN
SELECT INTO out_val in_val + 1;
END;
$function$
select out_test(3);
out_test
----------
4
根据文档 Returning:
The return value of a function cannot be left undefined. If control reaches the end of the top-level block of the function without hitting a RETURN statement, a run-time error will occur. This restriction does not apply to functions with output parameters and functions returning void, however. In those cases a RETURN statement is automatically executed if the top-level block finishes.