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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:23:33+00:00 2026-06-16T13:23:33+00:00

Currently working on a multi-tenant application, have an issue in generating identifier in a

  • 0

Currently working on a multi-tenant application, have an issue in generating identifier in a stored procedure.

I have a this table which has a meta information about tenant.

Tenant

TenantId
Name
IDPrefix,   -->Like SFT
IDStart     --> 000001

Client

TenantID
ClientIdentfier --> Like SFT000001

In a stored procedure I want to generate the next ClientIdentifier like SFT000002.

How can I do it based on last ClientIdentifier value + 1 ?

I know only taking last value with this below code.

select max(ClientID) + 1 from Client will give 1,2,etc

But I think I can’t do like

 DECLARE @CIdentier Varchar(50);

 select @CIdentier = select max(ClientIdentifier) + 1 from Client 
                                            to produce 'SFT000002

How can I do like this in a stored procedure?

Edit:

Tried mark_s answer and it worked like a charm!!!

  • 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-06-16T13:23:35+00:00Added an answer on June 16, 2026 at 1:23 pm

    This approach will be safe under load, e.g. it’s not going to return any duplicates, even if lots of client requests come in at the same time (this is “borrowed” from an answer by @remusrusanu to another question on SO).

    Basically, you need a sequence table with columns TenantID, TenantPrefix and CurrentValue and then you can use a stored procedure like this to safely fetch new values:

    -- add a IDValue column to your Tenant table
    ALTER TABLE dbo.Tenant
    ADD IDValue INT NOT NULL DEFAULT(0)
    
    -- create this procedure to fetch the next value for any given tenant
    CREATE PROCEDURE dbo.GetNextTenantID
       @tenantID INT,    
       @NextID VARCHAR(15) OUTPUT
    AS
       SET NOCOUNT ON;
    
       DECLARE @Out TABLE (NextVal INT, Prefix CHAR(3))
    
       UPDATE dbo.Tenant
       SET IDValue = IDValue + 1
       OUTPUT INSERTED.IDValue, INSERTED.IDPrefix INTO @Out(NextVal, prefix)
    
       SELECT TOP 1 @nextID = Prefix + CAST(NextVal AS VARCHAR(10)) FROM @Out
    GO
    

    The main point here is: you have to do the incrementing the IDValue and the returning of it inside a single UPDATE statement. Only with this approach can you be safe under load – all the approaches that have a SELECT first, increment, and then UPDATE are not safe and can return duplicates.

    Update: you cannot just include this snippet of code into a larger procedure of yours! Leave this procedure as is and just call it from your stored procedure – something like:

    ALTER PROCEDURE [dbo].[AddClient]  
    (
       @TenantId INT,
       @FirstName NVARCHAR(100),
       @LastName NVARCHAR(100),
       @ContactPerson NVARCHAR(100)
    )
    AS 
    BEGIN 
         SET NOCOUNT ON   
    
         IF @TenantId IS NULL 
            RAISERROR('The value for @TenantID should not be null', 15, 1) -- with log
    
         DECLARE @new_person_id INT
         DECLARE @new_patient_id INT
         DECLARE @ClientIdentifier NVARCHAR(50)
    
         -- call the stored procedure to get the next ClientIdentifier here
         EXEC dbo.GetNextTenantID @TenantID, @ClientIdentifier OUTPUT
    
         -- then go on and do your other lines of code from here on out .....
         ......
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a multi-tenant application that employs Shared DB/Shared Schema approach. IOW,
I'm currently working on a Sudoku application, the numbers are stored within a Multi-Dimensional
I have a multi-threaded Delphi 6 Pro application that I am currently working on
I am currently working on a project which supports multi-language. I have created a
We are currently working on a multi-tenant application that is running into some random
I'm currently working on a Java application which should have the capability to use
I am working on setting up a multi-tenant, seperate database application and have made
I am currently working on a multi-line Edit Text which might have placeholders within
Currently I am working on a multi-process desktop application on Windows. This application will
I'm currently working on a multi-threaded application, and I occasionally receive a concurrently modification

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.