Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 666043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:46:31+00:00 2026-05-13T23:46:31+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:46:31+00:00Added an answer on May 13, 2026 at 11:46 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table having hierarchical menus like id parent_id name 1 0 menu
i have a table with hierarchical structure. like this: (source: aspalliance.com ) and table
I have a SQL table like so: Update: I'm changing the example table as
I have the following SQL table: ObjectTable -------------------------------------------------- | ID | Name | Order
Development language and DB: PHP/MySQL I have a table geo_places with something like 8
I have a table reply of which structure is following: Id name parent_id ...
I have the following hierarchical table: Table Category: CategoryId, ParentCategoryId, CategoryName 1, null, SomeRoot
I have a hierarchical structure stored in a table. Each element has a pointer
I got a problem with Entity Framework 4.0 I have a hierarchical table Category:
I have a table containing hierarchical data. There are currently ~8 levels in this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.