CREATE TABLE public.tree_products ( product_id INTEGER DEFAULT nextval('ree_products_product_id_seq'::regclass) NOT NULL, name VARCHAR, parent_id INTEGER, CONSTRAINT ree_products_pkey PRIMARY KEY(product_id) ) CREATE OR REPLACE FUNCTION foo()RETURNS text AS $body$ DECLARE _row RECORD; _result text := ''; _child_row RECORD; _count integer := -1; _marker integer := 1; BEGIN FOR _row IN SELECT * FROM tree_products LOOP _result := _result || _marker || ' ' || _row.name; _count := (SELECT count(product_id) FROM tree_products WHERE parent_id = _row.product_id); IF _count > 0 THEN FOR _child_row IN SELECT * FROM tree_products WHERE parent_id = _row.product_id LOOP _result := _result || ' ' || _child_row.name; END LOOP; END IF; _marker := _marker =1; END LOOP; END; $body$ LANGUAGE plpgsql
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)