Question

I have a hierarchical table on Oracle pl/sql. something like:

create table hierarchical (
   id             integer primary key,
   parent_id          references hierarchical ,
   name           varchar(100));

I need to create a procedure to alter that table so I get a new field that tells, for each node, if it has any children or not.

Is it possible to do the alter and the update in one single procedure? Any code samples would be much appreciated.

Thanks

Was it helpful?

Solution

You can not do the ALTER TABLE (DDL) and the UPDATE (DML) in a single step.

You will have to do the ALTER TABLE, followed by the UPDATE.

BEGIN
  EXECUTE IMMEDIATE 'ALTER TABLE hierarchical ADD child_count INTEGER';
  --
  EXECUTE IMMEDIATE '
  UPDATE hierarchical h
  SET child_count = ( SELECT COUNT(*)
                      FROM hierarchical h2
                      WHERE h2.parent_id = h.id )';
END;

Think twice before doing this though. You can easily find out now if an id has any childs with a query.

This one would give you the child-count of all top-nodes for example:

SELECT h.id, h.name, COUNT(childs.id) child_count
FROM hierarchical h
LEFT JOIN hierarchical childs ON ( childs.parent_id = h.id )
WHERE h.parent_id IS NULL
GROUP BY h.id, h.name

Adding an extra column with redundant data will make changing your data more difficult, as you will always have to update the parent too, when adding/removing childs.

OTHER TIPS

If you just need to know whether children exist, the following query can do it without the loop or the denormalized column.

    select h.*, connect_by_isleaf as No_children_exist
      from hierarchical h
start with parent_id is null
connect by prior id = parent_id; 

CONNECT_BY_LEAF returns 0 if the row has children, 1 if it does not.

I think you could probably get the exact number of children through a clever use of analytic functions and the LEVEL pseudo-column, but I'm not sure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top