Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.48 KB | None | 0 0
  1. DROP FUNCTION IF EXISTS insert_node(CHARACTER VARYING,INTEGER,INTEGER);
  2. CREATE OR REPLACE FUNCTION insert_node(TABLE_NAME VARCHAR, id INT, pid INT) RETURNS void LANGUAGE PLPGSQL AS $$
  3. BEGIN
  4.     EXECUTE format(
  5.         'INSERT INTO %I_relation(ancestor, descendant, distance)
  6.        SELECT ancestor, $1 as descendant, distance + 1
  7.        FROM %I_relation WHERE descendant = $2
  8.        UNION ALL SELECT $3, $4, 0',
  9.         TABLE_NAME, TABLE_NAME
  10.     ) USING id, pid, id, id;
  11. END;
  12. $$;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement