Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Paged Results

Posted on 2009-05-19
4
Medium Priority
?
155 Views
Last Modified: 2012-05-07
I'm working with some SQL that uses true paging.
I would like to be able pass in a where clause and an order by to the stored procedure.
How can I implement t?
Can i build dynamic sql and then use sp_executesql ? Or is there another method?
ALTER PROCEDURE IMINVLOC_SQL_Paged
@Where varchar(2000) = NULL,
@OrderBy varchar(2000) = NULL,
@PageIndex int,
@PageSize int
AS
 
BEGIN
	DECLARE @SQL nvarchar(2000)
	DECLARE @Count int
	SELECT @Count = (SELECT COUNT(*) FROM IMINVLOC_SQL)
	
	DECLARE @StartRowIndex int
	SET @StartRowIndex = (@PageIndex * @PageSize) + 1;
	
	WITH Results AS (
		SELECT ROW_NUMBER() OVER (ORDER BY Item_No ASC) AS Row, Item_No, Item_Filler, Std_Cost
		FROM IMINVLOC_SQL
	)
	
	SELECT Item_No, Item_Filler, Std_Cost, @Count As TotalRecords
	FROM Results
	WHERE Row BETWEEN
	@StartRowIndex AND @StartRowIndex + @PageSize - 1
 
END

Open in new window

0
Comment
Question by:JRockFL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 400 total points
ID: 24427698
its better to use dynamic sql rather than putting a big case statement
0
 
LVL 8

Author Comment

by:JRockFL
ID: 24427728
thank you for your reply,
will i be able to put this in dynamic sql? I'm not familiar with the "WITH" statement.

WITH Results AS (
            SELECT ROW_NUMBER() OVER (ORDER BY Item_No ASC) AS Row, Item_No, Item_Filler, Std_Cost
            FROM IMINVLOC_SQL
      )
      
      SELECT Item_No, Item_Filler, Std_Cost, @Count As TotalRecords
      FROM Results
      WHERE Row BETWEEN
      @StartRowIndex AND @StartRowIndex + @PageSize - 1
0
 
LVL 39

Accepted Solution

by:
appari earned 1600 total points
ID: 24427834
yes you can include with in dynamic sql.
try like this

I didnt include checks on @OrderBy and @Where for zero length or null values.
add the checks within the procedure if you are not validating the input before you call the stored procedure.
ALTER PROCEDURE IMINVLOC_SQL_Paged
@Where varchar(2000) = NULL,
@OrderBy varchar(2000) = NULL,
@PageIndex int,
@PageSize int
AS
 
BEGIN
	DECLARE @SQL nvarchar(8000)
	DECLARE @Count int
	SELECT @Count = (SELECT COUNT(*) FROM IMINVLOC_SQL)
	
	DECLARE @StartRowIndex int
	SET @StartRowIndex = (@PageIndex * @PageSize) + 1;
	
	SET @SQL = ';WITH Results AS (
		SELECT ROW_NUMBER() OVER (ORDER BY ' + @OrderBy + ') AS Row, Item_No, Item_Filler, Std_Cost
		FROM IMINVLOC_SQL Where ' + @Where + ' )
	SELECT Item_No, Item_Filler, Std_Cost, @Count As TotalRecords
	FROM Results
	WHERE Row BETWEEN ' +  @StartRowIndex + ' AND ' +   @StartRowIndex + @PageSize - 1 ;
 
	Exec(@SQL);
 
END

Open in new window

0
 
LVL 8

Author Closing Comment

by:JRockFL
ID: 31583302
Thank you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

609 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