?
Solved

TOP and ROWCOUNT return different record sets

Posted on 2009-05-20
4
Medium Priority
?
338 Views
Last Modified: 2012-05-07
I have a query which can be written in two ways

Please refer the code snippet for Scenario 1 and 2

Both these scenarios are returning different record sets. Is this an expected behaviour?

Scenario 1
 
SELECT TOP(50) * from table order by column1 ASC
 
Scenario 2 
 
SET ROWCOUNT=50
SELECT * from table order by column1 ASC

Open in new window

0
Comment
Question by:Kanchipuramdeena
  • 2
4 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24431010
If you dont have proper order by clause then there can be discrepancies.

But it should return same set of result set provided same set of order by clause.
And one more thing,

SET ROWCOUNT=50

Above is incorrect and it should be

SET ROWCOUNT 50

Kindly try this one and provide us sample result set to disprove it.
0
 
LVL 1

Author Comment

by:Kanchipuramdeena
ID: 24431294
Sorry SET ROWCOUNT=50 was a typo

I am trying SET ROWCOUNT 50 only

The different queries are

SELECT TOP(50) * from table order by TimeStamp ASC

Take a note of ID value of the 50 row
for me its - {E17FF1A4-45CA-4CD8-A309-0D159403B021}
the execute

SET ROWCOUNT 50
SELECT * from table order by TimeStamp ASC

Now look at the value of ID at the 50th row
for me its - {E295779E-4D6E-4801-8628-8A3ABB4F767E}


dat.txt
0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 300 total points
ID: 24431386
Hello Kanchipuramdeena,

Try running these, and see if you get identical results:

SET ROWCOUNT 50
SELECT * from table order by TimeStamp ASC, ID ASC

SELECT TOP(50) * from table order by TimeStamp ASC, ID ASC

Regards,

Patrick
0
 
LVL 1

Author Closing Comment

by:Kanchipuramdeena
ID: 31583470
That's Great.

I was confused a bit... know i am clear...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question