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

  • Home
  • SEARCH
  • 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 686479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:01:43+00:00 2026-05-14T02:01:43+00:00

An Orders table has a CustomerId column and an OrderId column. For certain reasons

  • 0
  • An Orders table has a CustomerId column and an OrderId column.
  • For certain reasons it’s important that an order’s id is no longer than 2-bytes.
  • There will be several million orders in total, which makes 2-bytes not enough for a global order id.
  • A customer will have no more than several thousand orders making 2-bytes enough.
  • The obvious solution is to have the (CustomerId, CustomerOrderNumber) be unique rather than OrderId itself.

The problem is generating the next CustomerOrderId. Preferably, without creating a separate table for each customer (even if it contains only an IDENTITY column), in order to keep the upper layers of the solution simple.

Q: how would you generate the next OrderId so that (CustomerId, CustomerOrderId) is unique but CustomerOrderNumber itself is allowed to have repetitions? Does Sql Server 2008 have a built in functionality for doing this?

For lack of a better term I’m calling it a Compound IDENTITY column.

  • 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-14T02:01:44+00:00Added an answer on May 14, 2026 at 2:01 am

    try this:

    DECLARE @Output table (orderID smallint)  --smallint=2 bytes
    
    BEGIN TRANSACTION
    
    INSERT INTO ORDERS
            (CustomerId ,OrderId ,.....)
            OUTPUT INSERTED.OrderId 
            INTO @Output 
        SELECT
            @CustomerId ,ISNULL(MAX(OrderId),0)+1, @...
            FROM ORDERS WITH (UPDLOCK,HOLDLOCK)
            WHERE CustomerId=@CustomerId 
    
    --any other processing, can use just generated @Output.OrderId value if necessary
    
    COMMIT
    

    make sure you have a unqiue index/constraint on CustomerId ,OrderId

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

Sidebar

Related Questions

I have several USB mass storage flash drives connected to a Ubuntu Linux computer

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.