?
Solved

TOP and ROWCOUNT return different record sets

Posted on 2009-05-20
4
Medium Priority
?
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Suggested Courses

762 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