PGSQL CTE 递归 INSERT RETURNING 自动增量

PGSQL CTE recursive INSERT RETURNING autoincrement

我有:

CREATE TABLE public.treeview_menu_node (
    id int8 NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    parent_id int8 NULL,
    data jsonb NULL,
    name varchar NULL,
    caption varchar NULL,
    CONSTRAINT treeview_menu_node_pk PRIMARY KEY (id)
);

INSERT INTO public.treeview_menu_node
(parent_id, "name")
VALUES(NULL, 'node 1');

INSERT INTO public.treeview_menu_node
(parent_id, "name")
VALUES(1, 'node 1.1');

INSERT INTO public.treeview_menu_node
(parent_id, "name")
VALUES(1, 'node 1.2');

INSERT INTO public.treeview_menu_node
(parent_id, "name")
VALUES(NULL, 'node 2');

INSERT INTO public.treeview_menu_node
(parent_id, "name")
VALUES(4, 'node 2.1');

INSERT INTO public.treeview_menu_node
(parent_id, "name")
VALUES(4, 'node 2.2');

结构:

我需要的:

递归复制节点1到节点2

我的尝试:

WITH RECURSIVE r AS (
    INSERT INTO public.treeview_menu_node (parent_id, name, caption, data)
        SELECT new_parent_id, name, caption, data
        FROM (
            SELECT tmn.id, tmn.parent_id, :parent_id::BIGINT new_parent_id, tmn.name, tmn.caption, tmn.data
            FROM public.treeview_menu_node tmn
            WHERE id IN (:ids)
        ) t
    RETURNING id, parent_id, name, caption, data
    UNION ALL 
    INSERT INTO public.treeview_menu_node (parent_id, name, caption, data)
        SELECT new_parent_id, name, caption, data
        FROM (
            SELECT tmn.id, tmn.parent_id, r.id new_parent_id, tmn.name, tmn.caption, tmn.data
            FROM public.treeview_menu_node tmn
            JOIN r r ON r.id = tmn.parent_id 
        ) t
    RETURNING id, parent_id, name, caption, data
)
SELECT id, parent_id, name, caption, data
FROM r;

其中:

我得到的:

SQL 错误[42601]:语法错误(附近:“UNION”)位置:357


http://sqlfiddle.com/#!17/1e6fa/3

我找到了解决方案。

不允许在递归 CTE 中多次插入。而不是这个,使用函数:

NEXTVAL('table_sequensor_of_autoincrement')

函数 returns 一个新的 id,就像一个 INSERT 命令。因此,您可以使用单个 INSERT 命令准备整个数组以进行插入。

WITH RECURSIVE r AS (
    SELECT tmn.id, NEXTVAL('treeview_menu_id_seq') new_id, tmn.parent_id, :parent_id::BIGINT new_parent_id, tmn.name, tmn.caption, tmn.data
    FROM public.treeview_menu_node tmn
    WHERE id IN (:ids)
    UNION ALL 
    SELECT tmn.id, NEXTVAL('treeview_menu_id_seq') new_id, tmn.parent_id, r.new_id new_parent_id, tmn.name, tmn.caption, tmn.data
    FROM public.treeview_menu_node tmn
    JOIN r r ON r.id = tmn.parent_id 
)
INSERT INTO public.treeview_menu_node (id, parent_id, name, caption, data)
SELECT new_id, new_parent_id, name, caption, data
FROM r;

http://sqlfiddle.com/#!17/1e6fa/10