Solved

TOP and ROWCOUNT return different record sets

Posted on 2009-05-20
4
320 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 92

Accepted Solution

by:
Patrick Matthews earned 75 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server 2012 r2 and SQL 2014 6 34
SQL Server Code Optimization when a single Proc gets called multiples times inside another Stored Proc 8 40
Begin Transaction 12 25
Report 8 27
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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 setup several different housekeeping processes for a SQL Server.

732 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