Solved

Paged Results

Posted on 2009-05-19
4
149 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
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 400 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to enforce inte 8 43
Sql Data via Excel--performance issues 2 55
SQL Syntax:  How to Find Commonality Among Similar Results 2 43
Need help debbuging stored procedure 21 32
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

12 Experts available now in Live!

Get 1:1 Help Now