SQL server 2012 returns different results than SQL server 2008

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:

  • How can I check a uniqueidentifier is in a comma delimited list of ids?
  • Configuring ASP.NET MVC app's IIS 7.5 Application Pool identity as login on SQL Server 2008 R2
  • SQL Server Random Sort
  • select into in mysql
  • Print answer of switch case in SQL Server
  • Adding rows based on a condition sql
  • 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;
    IF @SocialSecurity = @ssn
    UPDATE kids
    SET Number = @Mem+1
    SET @Mem = @Mem+1
    SET @ssn = @SocialSecurity
    SET @Mem = 1
    FETCH NEXT FROM cur INTO @SocialSecurity, @PersonID, @Dep_SSN, @LastName, @FirstName, 
        @BirthDate, @Number;
    CLOSE 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.

  • SQL server casting string to integer checking value before casting
  • Properly closing a database connection - VBScript + MS SQL
  • Copy Data from a table in one Database to another separate database
  • Is there a faster method to update a new column from another column but from a previous row?
  • How to use SSIS to delete rows from a local table based on conditions from remote?
  • SQL Server select distinct hell
  • 2 Solutions collect form web for “SQL server 2012 returns different results than SQL server 2008”

    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.

    Your script requires that the cursor on kids groups records by the same SSN. But there is no ORDER BY to enforce this. Thus the records are being returned in some random order which messes up the @Mem counter.

    It’s not an issue with SQL 2012 vs. 2008. I believe you simply lucked out with SQL 2008 and the records were already ordered the way you wanted them, or SQL 2008 has a different behavior for selecting unordered results, or you defined the pk and indexes differently.

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.