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:
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.
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?
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
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.