EFileTahi-A Posted March 4, 2005 Posted March 4, 2005 Imagine that I have a table with 100 records and that I need to select all records from position 20 to 50... How? Thank you... Quote
EFileTahi-A Posted March 4, 2005 Author Posted March 4, 2005 (edited) I discovered the method... "SELECT * FROM tablename Limit x,y" :D - THREAD RESOLVED - Edited March 4, 2005 by EFileTahi-A Quote
OnTheAnvil Posted March 28, 2005 Posted March 28, 2005 I tried this with SQL Server 2000 and it gets a compilation error. Do you or anyone know of a way to do this with SQL Server? Right now if I want records 20-50 I select Top 50 records into a datatable and then delete the first 19. Thanks, OnTheAnvil Quote
Mister E Posted March 28, 2005 Posted March 28, 2005 This might be a way to do what you need:DECLARE @RangeStart int SET @RangeStart = 20 DECLARE @StartPoint int SET @StartPoint = (SELECT MAX([PrimaryKey]) FROM (SELECT TOP 20 [PrimaryKey] FROM [MyTable]) T) SELECT TOP 10 * FROM [MyTable] WHERE [PrimaryKey] > @StartPoint Quote
OnTheAnvil Posted March 28, 2005 Posted March 28, 2005 Thanks for the response. I had hoped for a simpler solution but I guess programming is never simple. This does assume that your [PrimaryKey] is incremental correct? ~OnTheAnvil This might be a way to do what you need:DECLARE @RangeStart int SET @RangeStart = 20 DECLARE @StartPoint int SET @StartPoint = (SELECT MAX([PrimaryKey]) FROM (SELECT TOP 20 [PrimaryKey] FROM [MyTable]) T) SELECT TOP 10 * FROM [MyTable] WHERE [PrimaryKey] > @StartPoint Quote
Mister E Posted March 28, 2005 Posted March 28, 2005 Yes, the [PrimaryKey] field would be an auto-incrementing integer IDENTITY field. You could easily adjust the numbers to fit your desired range. Quote
bshaen Posted March 29, 2005 Posted March 29, 2005 Imagine that I have a table with 100 records and that I need to select all records from position 20 to 50... How? Thank you... you may using Between or In method in sql query, SELECT * FROM Table_name WHERE Code BETWEEN "20" AND "50" or SELECT * FROM Table_name WHERE Code IN "20" TO "50" Quote
Mister E Posted March 29, 2005 Posted March 29, 2005 you may using Between or In method in sql query, SELECT * FROM Table_name WHERE Code BETWEEN "20" AND "50" or SELECT * FROM Table_name WHERE Code IN "20" TO "50"Well that assumes that "Code" is completely sequential, which is not guarenteed. If there are any gaps in "Code" between 20 and 50, then you're not going to get a range of 30 records. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.