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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:31:19+00:00 2026-06-14T08:31:19+00:00

To assist in understanding I have a table like this: itemcode itemname icode serialnum

  • 0

To assist in understanding I have a table like this:

itemcode itemname icode serialnum
1        A        10    0
2        B        10    0
3        C        10    0
4        D        11    0
5        E        13    0
6        F        20    0
7        G        20    0

I want the result to look like the table below using a single update query with the help of looping cursors:

itemcode itemname icode serialnum
1        A        10    1
2        B        10    2
3        C        10    3
4        D        11    1
5        E        13    1
6        F        20    1
7        G        20    2

Item code is the primary key in this table. The logic behind generating the serial number is whenever icode changes the serial number gets reset to 1. I need a single update query to update the table with the help of cursors if someone can assist with a solution?

  • 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-14T08:31:21+00:00Added an answer on June 14, 2026 at 8:31 am

    So you want a sequential serial number for every icode-group. Then you can use ROW_NUMBER with PARTITION BY icode:

    WITH CTE AS
    (
      SELECT itemcode, itemname, icode, serialnum,
         ,   ROW_NUMBER() OVER (PARTITION BY icode ORDER BY itemcode) AS RN
      FROM dbo.TableName 
    )
    UPDATE CTE SET serialnum = RN;
    

    If you want it to be reculculated on every update you could use a trigger:

    CREATE TRIGGER dbo.TableName_Updated
    ON dbo.TableName 
    FOR UPDATE /* Fire this trigger when one or multiple rows are UPDATEd */
    AS BEGIN
        WITH CTE AS
        (
          SELECT itemcode, itemname, icode, serialnum,
             ,   ROW_NUMBER() OVER (PARTITION BY icode ORDER BY itemcode) AS RN
          FROM dbo.TableName t INNER JOIN INSERTED i 
            ON t.itemcode = i.itemcode
        )
        UPDATE CTE SET serialnum = RN
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm hoping someone can assist me with understanding if/how something like this is possible.
Please assist with MVVM design/understanding problem. Given that we have a Windows Phone app
Can anyone assist me in connecting via Management Studio using the hostname provided by
could anyone assist me with this. I need to compare two beefy xml files
can anyone assist me in finding the proper formula for quad normalization ? using
Can someone assist in creating a Regex for the following situation: I have about
I'm developing a TFS tool to assist the developers in our company. This said
To assist in our ability to step through code when debugging I'd like to
Would someone kindly assist me with the following? I have two DataGridView objects that
I'm using Visual Assist X trial on VS2010 Pro. When I do extract method

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.