Question

I am pretty new in Microsoft SQL Server and I am not so into DB.

I have a doubt related to this query (that is executed inside the code of a web application on which I am working on):

SELECT * FROM VulnerabilityAlertDocument WHERE (1=1) ORDER BY Published DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;

From what I understand this return me all the fields of 10 rows in my table (ordered in descending according to the value of the value of the Published column)

So in my web application it is used to obtain some paged view.

For example I have a view that show me a table that at the beginning contains the first 10 records, then I can click on a "Next" button, the query is executed again and I show the records between the 11 to the 20 and so again...

My doubt is: why if I execute this query into Microsoft SQL Manager Stusio the first time I obtain the first 10 records but if I try to execute it again I don't obtain the records between 10 to 20 but I obtain again the first 10 records?

Was it helpful?

Solution

You can find very good documentation on the SQL Server syntax on MSDN: http://technet.microsoft.com/en-us/library/gg699618.aspx

In short, OFFSET 0 ROWS means start at row 0, if you set the offset to 10 you'll get the next set of 10 rows:

SELECT * FROM VulnerabilityAlertDocument WHERE (1=1) ORDER BY Published DESC OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;

SQL Server does not keep track of which rows you queried already, that's up to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top