MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 23rd, 2012, 02:21 PM
sqgs sqgs is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 69 sqgs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 52 m 49 sec
Reputation Power: 9
SQL server 2012 returns different result than SQL server 2008

Hi,
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), @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;

Last edited by sqgs : October 23rd, 2012 at 03:09 PM.

Reply With Quote
  #2  
Old October 24th, 2012, 06:05 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,351 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 8 h 2 m 22 sec
Reputation Power: 390
The problem is that you don't have an order by clause in the cursor declaration. Anyhow, you don't need a cursor at all.

Code:
update kids
   set Number = row_number() over (partition by ssn order by kid_dob desc)

Last edited by swampBoogie : October 24th, 2012 at 06:08 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > SQL server 2012 returns different result than SQL server 2008

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap