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 591801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:40:22+00:00 2026-05-13T15:40:22+00:00

Suppose the table with two columns: ParentEntityId int foreign key Number int ParentEntityId is

  • 0

Suppose the table with two columns:

ParentEntityId int foreign key
Number int

ParentEntityId is a foreign key to another table.

Number is a local identity, i.e. it is unique within single ParentEntityId.

Uniqueness is easily achieved via unique key over these two columns.

How to make Number be automatically incremented in the context of the ParentEntityId on insert?


Addendum 1

To clarify the problem, here is an abstract.

ParentEntity has multiple ChildEntity, and each ChiildEntity should have an unique incremental Number in the context of its ParentEntity.


Addendum 2

Treat ParentEntity as a Customer.

Treat ChildEntity as an Order.

So, orders for every customer should be numbered 1, 2, 3 and so on.

  • 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-13T15:40:22+00:00Added an answer on May 13, 2026 at 3:40 pm

    Well, there’s no native support for this type of column, but you could implement it using a trigger:

    CREATE TRIGGER tr_MyTable_Number
    ON MyTable
    INSTEAD OF INSERT
    AS
    
    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
    
    BEGIN TRAN;
    
    WITH MaxNumbers_CTE AS
    (
        SELECT ParentEntityID, MAX(Number) AS Number
        FROM MyTable
        WHERE ParentEntityID IN (SELECT ParentEntityID FROM inserted)
    )
    INSERT MyTable (ParentEntityID, Number)
        SELECT
            i.ParentEntityID,
            ROW_NUMBER() OVER
            (
                PARTITION BY i.ParentEntityID
                ORDER BY (SELECT 1)
            ) + ISNULL(m.Number, 0) AS Number
        FROM inserted i
        LEFT JOIN MaxNumbers_CTE m
            ON m.ParentEntityID = i.ParentEntityID
    
    COMMIT
    

    Not tested but I’m pretty sure it’ll work. If you have a primary key, you could also implement this as an AFTER trigger (I dislike using INSTEAD OF triggers, they’re harder to understand when you need to modify them 6 months later).

    Just to explain what’s going on here:

    • SERIALIZABLE is the strictest isolation mode; it guarantees that only one database transaction at a time can execute these statements, which we need in order to guarantee the integrity of this “sequence.” Note that this irreversibly promotes the entire transaction, so you won’t want to use this inside of a long-running transaction.

    • The CTE picks up the highest number already used for each parent ID;

    • ROW_NUMBER generates a unique sequence for each parent ID (PARTITION BY) starting from the number 1; we add this to the previous maximum if there is one to get the new sequence.

    I probably should also mention that if you only ever need to insert one new child entity at a time, you’re better off just funneling those operations through a stored procedure instead of using a trigger – you’ll definitely get better performance out of it. This is how it’s currently done with hierarchyid columns in SQL ’08.

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

Sidebar

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.