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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745673692a4639557.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