Solved

Paged Results

Posted on 2009-05-19
4
148 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
  • 2
4 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 100 total points
Comment Utility
its better to use dynamic sql rather than putting a big case statement
0
 
LVL 8

Author Comment

by:JRockFL
Comment Utility
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 400 total points
Comment Utility
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
Comment Utility
Thank you!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now