Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

MS SQL OFFSET

MS SQL and Oracle

To page through results you can either use the OFFSET keyword in conjunction with the FETCH keyword. Cannot be used with TOP.

SELECT <columns> FROM <table> OFFSET <skipped rows> ROWS FETCH NEXT <# of rows> ROWS ONLY;

I have tried this and it didn't work on MS SQL server 2014

Can you please show us a demo of how to make it work?

1 Answer

Steven Parker
Steven Parker
229,732 Points

FETCH is a T-SQL operator for use with a cursor. Are you sure that's what you need? If so, please show the complete T-SQL where you establish the cursor.

If you just want a limited number of rows from an ordinary query you should use TOP instead.

I have no idea what T-SQL means. I am still new at this.

I am trying to do as we were taught in the video that you can use OFFSET in order to start in a certain row and then start printing the next limited rows. How can I do that in MS SQL Server?

Steven Parker
Steven Parker
229,732 Points

Note that using both TOP and OFFSET requires a sub-query, and OFFSET requires ORDER BY:

SELECT TOP <# of rows> * FROM
    (SELECT <columns> FROM <table> ORDER BY <columname> OFFSET <skipped rows> ROWS);