admin管理员组文章数量:1435859
I have write PostgreSQL query what working perfectly:
I try to convert this query to function
DROP FUNCTION path1(character varying,character varying);
CREATE
OR REPLACE FUNCTION path1(enter character varying, request character varying) RETURNS TABLE (
reti INTEGER,
retid character varying,
retname character varying,
retmime character varying,
retpath character varying
) AS $$
BEGIN
WITH RECURSIVE x AS (
SELECT i, parent, id, name, type, '' AS path
FROM entry
WHERE name = enter
UNION ALL
SELECT e.i, e.parent, e.id, e.name, e.type, (x.path || '/' || e.name) AS path
FROM entry e, x
WHERE x.id = e.parent
)
SELECT x.i as reti, x.id as retid, x.name as retname, types.mime as retmime, x.path as retpath
FROM x
JOIN types ON types.i = x.type
WHERE x.path = request;
END;
$$ LANGUAGE plpgsql;
GRANT EXECUTE ON FUNCTION path1(enter character varying, request character varying) TO public;
and call it
select * from public.PATH1 ('JS-VBNET-2', '/Index.htm');
But I get trouble
ERROR: 42601: 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 path1(character varying,character varying) line 3 at SQL statement
AI hint is:
Add a destination for the result data, such as using INTO or returning the results in a SELECT statement.
What going wrong? How I can convert my function correctly?
I have write PostgreSQL query what working perfectly:
I try to convert this query to function
DROP FUNCTION path1(character varying,character varying);
CREATE
OR REPLACE FUNCTION path1(enter character varying, request character varying) RETURNS TABLE (
reti INTEGER,
retid character varying,
retname character varying,
retmime character varying,
retpath character varying
) AS $$
BEGIN
WITH RECURSIVE x AS (
SELECT i, parent, id, name, type, '' AS path
FROM entry
WHERE name = enter
UNION ALL
SELECT e.i, e.parent, e.id, e.name, e.type, (x.path || '/' || e.name) AS path
FROM entry e, x
WHERE x.id = e.parent
)
SELECT x.i as reti, x.id as retid, x.name as retname, types.mime as retmime, x.path as retpath
FROM x
JOIN types ON types.i = x.type
WHERE x.path = request;
END;
$$ LANGUAGE plpgsql;
GRANT EXECUTE ON FUNCTION path1(enter character varying, request character varying) TO public;
and call it
select * from public.PATH1 ('JS-VBNET-2', '/Index.htm');
But I get trouble
ERROR: 42601: 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 path1(character varying,character varying) line 3 at SQL statement
AI hint is:
Add a destination for the result data, such as using INTO or returning the results in a SELECT statement.
What going wrong? How I can convert my function correctly?
Share Improve this question edited Nov 16, 2024 at 7:17 Alex161 asked Nov 15, 2024 at 19:43 Alex161Alex161 719 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 0So, firstly, Supabase SQL editor allow create function, don't need to use special form "New Function" in Supabase. Function visible in Supabase locally and remotelly:
And secondary, after some experiment I understand troubles. Correct workable functions is
DROP FUNCTION path1(character varying,character varying);
CREATE
OR REPLACE FUNCTION path1 (
enter CHARACTER VARYING,
request CHARACTER VARYING
) RETURNS TABLE (
reti INTEGER,
retid CHARACTER VARYING,
retname CHARACTER VARYING,
retmime CHARACTER VARYING,
retpath text
) AS $$
BEGIN
RETURN QUERY
WITH RECURSIVE x AS (
SELECT i, parent, id, name, type, '' AS path
FROM entry
WHERE name = enter
UNION ALL
SELECT e.i, e.parent, e.id, e.name, e.type, (x.path || '/' || e.name) AS path
FROM entry e, x
WHERE x.id = e.parent
)
SELECT x.i AS reti, x.id AS retid, x.name AS retname, types.mime AS retmime, x.path AS retpath
FROM x
JOIN types ON types.i = x.type
WHERE x.path = request;
END;
$$ LANGUAGE plpgsql;
GRANT EXECUTE ON FUNCTION path1(enter character varying, request character varying) TO public;
Key feature of changing is strange, when PostgreSQL combined character varying fields - result is "text", not character varying.
and strange response was because I miss "Return Query" before CTE definition
本文标签: How to convert recursive PostgreSQL request to functionStack Overflow
版权声明:本文标题:How to convert recursive PostgreSQL request to function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745673691a2669722.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Procedure
is a different thing then aFunction
. Also anSQL
procedure/function is different then what you are using which isplpgsql
. Edit the question to get your terms correct. – Adrian Klaver Commented Nov 15, 2024 at 19:50