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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:21:25+00:00 2026-06-03T10:21:25+00:00

I need to use an existing table like this to facilitate rolling counters: –

  • 0

I need to use an existing table like this to facilitate rolling counters: –

CREATE TABLE [dbo].[IdentityCounter](
    [CounterName] [nvarchar](255) NOT NULL,
    [StartId] [bigint] NOT NULL,
    [EndId] [bigint] NOT NULL,
    [CurrentId] [bigint] NOT NULL,
    [CreatedTime] [datetime] NULL,
    [ModifiedTime] [datetime] NULL,
    [Description] [nvarchar](255) NULL
) ON [PRIMARY]

For example say I have the following: –

 CounterName  "ReceiptNumber"
 StartId  1
 EndId  99999 
 etc. etc.

For a particular CounterName I want to be able to call a stored procedure or function to: –

  • To get me the next value and save that back to CurrentId and send the CurrentId back as the result

  • If the next value equals EndId + 1 then I need to make the next value equal to StartId and save that back to CurrentId. Also send the CurrentId back as the result.

  • There will be multiple applications calling this so need to ensure things remain consistent.

The counter is not a key it’s just something that needs to be passed to a 3rd party service.

What’s the best approach?

Thanks! ITG

  • 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-03T10:21:27+00:00Added an answer on June 3, 2026 at 10:21 am

    First, I declare data that looks like the data in your question. It would be better if you declared this in your question so that we are all working with the same data. The data has one difference: the EndID is 5 instead of 99999 for demonstration purposes.

    INSERT INTO dbo.IdentityCounter(
      CounterName,
      StartID,
      EndID,
      CurrentID
    )
    VALUES (
      N'ReceiptNumber',
      1,
      5,
      1
    );
    

    This procedure does what you ask for:

    CREATE PROCEDURE dbo.IncrementCounter (
      @CounterName NVARCHAR(255)
    )
    AS
    BEGIN
      SET NOCOUNT ON;
    
      UPDATE dbo.IdentityCounter
      SET CurrentID = CASE WHEN CurrentID = EndID THEN StartID ELSE CurrentID + 1 END
      OUTPUT DELETED.CurrentID
      WHERE CounterName = @CounterName;
    END;
    

    It returns a ‘scalar’ result set (one column, one row), which represents the value of CurrentID for the given CounterName before the counter is incremented.

    Example of repeated execution:

    EXEC dbo.IncrementCounter
      @CounterName = 'ReceiptNumber';
    EXEC dbo.IncrementCounter
      @CounterName = 'ReceiptNumber';
    EXEC dbo.IncrementCounter
      @CounterName = 'ReceiptNumber';
    EXEC dbo.IncrementCounter
      @CounterName = 'ReceiptNumber';
    EXEC dbo.IncrementCounter
      @CounterName = 'ReceiptNumber';
    EXEC dbo.IncrementCounter
      @CounterName = 'ReceiptNumber';
    EXEC dbo.IncrementCounter
      @CounterName = 'ReceiptNumber';
    EXEC dbo.IncrementCounter
      @CounterName = 'ReceiptNumber';
    EXEC dbo.IncrementCounter
      @CounterName = 'ReceiptNumber';
    EXEC dbo.IncrementCounter
      @CounterName = 'ReceiptNumber';
    

    Should output ten result sets similar to this sequence: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5.

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

Sidebar

Related Questions

I have two already-existing tables which look (in part) roughly like this: CREATE TABLE
When we use CTAS like this: create table big2 as select * from big1;
I need help to use jConfirm with this existing code (php & Jquery &
I have an existing table that I'd like to use for a Rails application.
I need to use a plist from an existing iOS app in my android
So, I need use this event so I can navigate trought blog posts. I
I need to use this formula to calculate the upper limit and lower limit
I need to use a forward chainer in certain Prolog problem. I would like
Need to use own imaged markers instead built-in pins. I have several questions. 1.
I need to use a C library to access and older file format used

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.