I have written a PostgreSQL query that's working perfectly:
WITH RECURSIVE x AS (
SELECT i, parent, id, name, type, '' AS path
FROM entry
WHERE name = 'JS-VBNET-2'
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 = '/Index.htm';
I tried to convert this query to a 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;
Call:
select * from public.PATH1 ('JS-VBNET-2', '/Index.htm');
But I get an error:
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 is going wrong? How I can convert my function correctly?