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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:03:29+00:00 2026-05-25T21:03:29+00:00

I am new to web application programming and handling concurrency using an RDBMS like

  • 0

I am new to web application programming and handling concurrency using an RDBMS like SQL Server. I am using SQL Server 2005 Express Edition.

I am generating employee code in which the last four digits come from this query:

SELECT max(ID) FROM employees WHERE district = "XYZ";

I am not following how to handle issues that might arise due to concurrent connections. Many users can pick same max(ID) and while one user clicks “Save Record”, the ID might have already been occupied by another user.

How to handle this issue?

  • 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-25T21:03:30+00:00Added an answer on May 25, 2026 at 9:03 pm

    Here are two ways of doing what you want. The fact that you might end up with unique constraint violation on EmpCode I will leave you to worry about :).

    1. Use scope_identity() to get the last inserted ID and use that to calculate EmpCode.

    Table definition:

    create table Employees
    (
      ID int identity primary key,
      Created datetime not null default getdate(),
      DistrictCode char(2) not null,
      EmpCode char(10) not null default left(newid(), 10) unique
    )
    

    Add one row to Employees. Should be done in a transaction to be sure that you will not be left with the default random value from left(newid(), 10) in EmpCode:

    declare @ID int
    
    insert into Employees (DistrictCode) values ('AB')
    
    set @ID = scope_identity()
    
    update Employees
    set EmpCode = cast(year(Created) as char(4))+DistrictCode+right(10000+@ID, 4)
    where ID = @ID 
    

    2. Make EmpCode a computed column.

    Table definition:

    create table Employees
    (
      ID int identity primary key,
      Created datetime not null default getdate(),
      DistrictCode char(2) not null,
      EmpCode as cast(year(Created) as char(4))+DistrictCode+right(10000+ID, 4) unique
    )
    

    Add one row to Employees:

    insert into Employees (DistrictCode) values ('AB')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to programming. I am developing a web application using C#, where
I'm relatively new to web application programming so I hope this question isn't too
We are programming a new web application framework ( Second WAF ). I was
we are currently working on a new web application using Java and MySql. We
I am using the webapplicationbuilder class to create a new web application and have
I'm programming a new free (free speach) web application for managing Medical (clinical) Labs
I am starting a new web application in PHP and this time around I
I'm designing a new web application and I'm aware that the process of registration
I have started a new web application and have decided to use jquery as
I am starting to build a new web application that will require user accounts.

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.