Postgresql 这是我之前的评论。 create table demoTextTable ( text_id serial primary key, text_details character varying, new_text_id integ

Postgresql 这是我之前的评论。 create table demoTextTable ( text_id serial primary key, text_details character varying, new_text_id integ,postgresql,function,recursion,common-table-expression,Postgresql,Function,Recursion,Common Table Expression,这是我之前的评论。 create table demoTextTable ( text_id serial primary key, text_details character varying, new_text_id integer ) insert into demoTextTable(text_details, new_text_id) values ('Comment 1', 2), ('Comment 1 updated 1st Time',3

这是我之前的评论。
create table demoTextTable
(
    text_id serial primary key,
    text_details character varying,
    new_text_id integer
)

insert into demoTextTable(text_details, new_text_id)
values ('Comment 1', 2),
       ('Comment 1 updated 1st Time',3),
       ('Comment 1 updated 2nd Time',4),
       ('Comment 1 updated 3rd Time',5),
       ('Comment 1 updated 4th Time',5);
create or replace function get_text_history(textId integer)
RETURNS Table (
    text_id integer,
    text_details varchar,
    new_text_id integer)
AS $$
BEGIN
WITH RECURSIVE textHierarchy AS (

    select tm.text_id, tm.text_details, tm.new_text_id
    from text_master tm where tm.text_id = textId

    UNION ALL

    select tm.text_id, tm.text_details, tm.new_text_id
    FROM textHierarchy AS txtHr, text_master AS tm where tm.text_id != txtHr.new_text_id
    and txtHr.new_text_id is not null
)
select * from textHierarchy;
END;
$$ Language plpgsql;
ERROR:  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 get_text_history(integer) line 3 at SQL statement
SQL state: 42601
create or replace function get_text_history(textId integer)
RETURNS Table (
    text_id integer,
    text_details varchar,
    new_text_id integer)
AS $$
BEGIN
WITH RECURSIVE textHierarchy AS (

    select dtbl.text_id, dtbl.text_details, dtbl.new_text_id
    from demoTextTable dtbl where dtbl.text_id = textId

    UNION

    select dtbl.text_id, dtbl.text_details, dtbl.new_text_id
    FROM demoTextTable dtbl where dtbl.text_id != dtbl.new_text_id or 
    dtbl.text_id = dtbl.new_text_id order by text_id asc

)
select * from textHierarchy;
END;
$$ Language plpgsql;
CREATE OR REPLACE FUNCTION get_text_history(textId integer)
RETURNS TABLE (
    text_id integer,
    text_details varchar,
    new_text_id integer
) LANGUAGE sql AS
$$WITH RECURSIVE textHierarchy AS (
    SELECT tm.text_id, tm.text_details, tm.new_text_id
    FROM demotexttable tm 
    WHERE tm.text_id = textId
  UNION
    SELECT tm.text_id, tm.text_details, tm.new_text_id
    FROM textHierarchy AS txtHr 
        JOIN demotexttable AS tm ON tm.text_id = txtHr.new_text_id
    WHERE txtHr.new_text_id IS NOT NULL
)
SELECT * FROM textHierarchy$$;