Solved

TOP and ROWCOUNT return different record sets

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

808 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