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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:27:59+00:00 2026-06-14T09:27:59+00:00

I need to increment a column (Counts) +1 for every NEW DocNumber. So if

  • 0

I need to increment a column (Counts) +1 for every NEW DocNumber. So if the DocNumber is the same for three columns, the counter will be 1,2,3 etc. Below is the code. I got it to the point where it will update the entire table, just not based on the same or different docnumbers. Can someone help me with the logic here?

Basically, I want to restart the counter when it hits a new TempDocNumber

DECLARE @DocTable Table (DetailID FLOAT, TempDocNumber FLOAT, Counts INT)
INSERT INTO @DocTable (DetailID, TempDocNumber, Counts)
SELECT DetailID, DocumentNumber, 0 FROM ChargebackDetailTempTable


declare @Counter int
Set @Counter = 0
Update @Doctable
SET @Counter = counts = @Counter + 1

-- start counter over when new tempdocnumber
  • 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-14T09:28:00+00:00Added an answer on June 14, 2026 at 9:28 am

    You could just create your insert like this:

    DECLARE @DocTable Table (DetailID FLOAT, TempDocNumber FLOAT, Counts INT)
    INSERT INTO @DocTable (DetailID, TempDocNumber, Counts)
    SELECT  DetailID, 
            DocumentNumber TempDocNumber, 
            ROW_NUMBER() OVER(PARTITION BY DocumentNumber ORDER BY DetailId) Counts
    FROM ChargebackDetailTempTable
    

    No UPDATE needed.

    For SQL Server 2000

    Assuming that DetailID is an identity column, you can try the following (again, no UPDATE needed):

    DECLARE @DocTable Table (DetailID FLOAT, TempDocNumber FLOAT, Counts INT)
    INSERT INTO @DocTable (DetailID, TempDocNumber, Counts)
    SELECT  DetailID, 
            TempDocNumber,
            Counts = (  SELECT COUNT(T1.DetailID) + 1
                        FROM ChargebackDetailTempTable T1
                        WHERE T1.DetailID < T.DetailID and T1.TempDocNumber = T.TempDocNumber)
    FROM ChargebackDetailTempTable T
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to generate a unique value for a column. I want to increment
I need to increment a float value atomically. I get its int value by
I need to get the number after the button to increment in a for
Basically I need to reset Identity Increment for all tables to its original. Here
I have table with an auto-increment field, but I need to transfer the table
Is there a integer element for a cookie? I need the cookie to increment
I have two mysql tables, one needs to start its auto-increment column id with
I have a table that has a Key column and a Counter column. I
Is there a way to reserve or skip or increment value of identity column?
I have an entity with a descriptor and a count. I need to increment

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.