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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:55:39+00:00 2026-06-13T06:55:39+00:00

I have a piece of script using cursor to sequence rows in a table.

  • 0

I have a piece of script using cursor to sequence rows in a table. For example, the table looks like this,
SSN, Kid_SSN, Kid_DOB, Seq# to list every person with 1 or more kids. I want to update Seq# to label each kid as 1,2,3… based on Date Of Birth.
I used cursor for update to get it done successfully in SQL server 2008. My current trouble is that this same script wouldn’t run as expected in sql server 2012. The problem is in SQL 2012 cursor fetch NEXT more than 1 row at a time. So question is where can I set the cursor fetch size? I searched around but came up no good answer.
Anyone here can shed some light? thanks.

Scrip looks like this:

DECLARE @SocialSecurity varchar(9), @PersonID int, @Dep_SSN varchar(9)
DECLARE @LastName varchar(20), @FirstName varchar(20), @BirthDate datetime, 
    @Number int

DECLARE @ssn varchar(9) = '000000000'
DECLARE @Mem int = 1
DECLARE cur cursor
FOR SELECT * FROM kids
FOR UPDATE OF Number;

OPEN cur;

FETCH NEXT FROM cur INTO @SocialSecurity, @PersonID, @Dep_SSN, @LastName, @FirstName, 
    @BirthDate, @Number;

WHILE @@FETCH_STATUS = 0
BEGIN

IF @SocialSecurity = @ssn
BEGIN

UPDATE kids
SET Number = @Mem+1
WHERE CURRENT OF cur
SET @Mem = @Mem+1
END;
ELSE
BEGIN
SET @ssn = @SocialSecurity
SET @Mem = 1
END;
FETCH NEXT FROM cur INTO @SocialSecurity, @PersonID, @Dep_SSN, @LastName, @FirstName, 
    @BirthDate, @Number;
END;
CLOSE cur;
DEALLOCATE cur;

More info on this problem.
kids is a temp table generated by ‘SELECT .. INTO’ and a simplified set looks like this

SSN       Kid_SSN        DOB        Seq#
123123123 987987987      1/1/2000   1
123123123 987987988      1/1/2003   1
123123125 890890890      2/3/2002   1

So all seq# are initiated to 1. After going through the script above, I expect table kids to
look like this

SSN       Kid_SSN        DOB        Seq#
123123123 987987987      1/1/2000   1
123123123 987987988      1/1/2003   2
123123125 890890890      2/3/2002   1

The script ran perfectly to achieve this on server 2008 R2, but not on server 2012. Furthermore, I found that it only updated row 88, 176 and so on, if applicable. That is why I think cursor fetch 88 rows at a time. But on server 2008 it apparently fetch 1 row at a time as I expected. Hope this will explain the problem I have.
I want to force cursor fetching 1 row at a time to make it work on server 2012, though it’s not efficient. Or, how to do sequencing without using cursor? Thanks.

  • 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-13T06:55:40+00:00Added an answer on June 13, 2026 at 6:55 am

    Would it work to replace the entire script with a rank over function?

    Something like

    select *, rank() over (partition by SSN, Kid_DOB order by Kid_SSN) as SeqNum
    from kids
    

    I think this should work and if not you can tweak the partition and order by to make it work.

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

Sidebar

Related Questions

I have a piece of code that looks like this: downloadsByExtensionCount = defaultdict(int) downloadsByExtensionList
I have been using this piece of script to load external .js files to
I have a piece of code like this: <div id=container runat=server> <div id=parent runat=server>
In javascript suppose you have this piece of code: <div> <script type=text/javascript>var output =
I have a piece of code working in knockout.js like this: <div> ... some
I have this piece of code written in jQuery and PHP: <script type=text/javascript> $(document).ready(function(){
I have this piece of code in PHP and using a PostgreSQL as the
Hello everyone Masters Of Web Delevopment :) I have a piece of PHP script
I have this piece of code: Page.ClientScript.RegisterClientScriptInclude(this.GetType(), Guid.NewGuid().ToString(), this.ResolveUrl(~/Scripts/min.js)); When we push to our
Here I have piece of code, showing example of how I want to update

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.