We help IT Professionals succeed at work.
Get Started

SQL stored procedure must return varying record counts starting at varying record numbers

367 Views
Last Modified: 2012-05-12
Can I create a SQL stored procedure that willl return a requested quantity of records and where the starting record may change with each call.  Picture a website where users will see the first 25 results, click next and get the next 25, etc. and then click back and see the previous 25, etc.  Also, they can change the number of results they see at once, so they might require results 1-40, then 41-80, etc.  

The query will be complicated and there won't be a column to identify record numbers in any tables, meaning the query may build hundreds of records that will be unique with each call and no preexisting means to identify record numbers.

I picture a simple example to be something like this

CREATE PROCEDURE [BuildItems]
      @nStartingRecord AS INT,
      @nNumberOfRecordsToReturn AS INT,
      @cKeyItemCategories AS UNIQUEIDENTIFIER

SELECT * FROM Items
      WHERE Items.keyItemCategories = @cKeyItemCategories AND
      RecordNumber >= @nStartRecord AND RecordNumber <= @nStartRecord+@nRecordCount
Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2011
Commented:
This problem has been solved!
Unlock 3 Answers and 4 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE