Important alert: (current site time 7/15/2013 7:48:28 PM EDT)
 

VB icon

Breaking a table into pages

Email
Submitted on: 4/4/2003 11:08:14 AM
By: MstrControl  
Level: Intermediate
User Rating: By 3 Users
Compatibility: SQL Server 2000, SQL Server 7.0, SQL Server 6.5 and earlier, Oracle
Views: 66257
(About the author)
 
     Reading the paging technique writen by Marcio Coleho here on PSC, I found a little flaw in the method. Althought the code did break the table into pages, it didn't bring them in the correct order, if you want to use other field than PK. I fixed this little flaw, but it still need a SINGLE PRIMARY or ALERNATE KEY. The original Coelho's code can be found at http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=652&lngWId=5
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Breaking a table into pages
-- Description:Reading the paging technique writen by Marcio Coleho here on PSC, I found a little flaw in the method.
Althought the code did break the table into pages, it didn't bring them in the correct order, if you want to use other field than PK.
I fixed this little flaw, but it still need a <B style="color:red">SINGLE</B> <B>PRIMARY</B> or <B>ALERNATE</B> KEY.
The original Coelho's code can be found at http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=652&lngWId=5
-- By: MstrControl
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=656&lngWId=5--for details.--**************************************

DROP PROCEDURE sp_Table_Paging 
go 
CREATE PROCEDURE sp_Table_Paging 
	@Page 			int, 
	@RecsPerPage 	int, 
	@Fields			varchar(2000),
	@Table 			sysname,
	@Key 			sysname
AS 
/*
exec sp_Table_Paging 1, 100, 'CustomerID, CompanyName', 'Customers', 'CompanyName'
exec sp_Table_Paging 2, 100, 'CustomerID, CompanyName', 'Customers', 'CompanyName'
exec sp_Table_Paging 3, 100, 'CustomerID, CompanyName', 'Customers', 'CompanyName'
exec sp_Table_Paging 4, 100, 'CustomerID, CompanyName', 'Customers', 'CompanyName'
exec sp_Table_Paging 5, 100, 'CustomerID, CompanyName', 'Customers', 'CompanyName'
exec sp_Table_Paging 6, 100, 'CustomerID, CompanyName', 'Customers', 'CompanyName'
exec sp_Table_Paging 7, 100, 'CustomerID, CompanyName', 'Customers', 'CompanyName'
exec sp_Table_Paging 8, 100, 'CustomerID, CompanyName', 'Customers', 'CompanyName'
exec sp_Table_Paging 9, 100, 'CustomerID, CompanyName', 'Customers', 'CompanyName'
*/
DECLARE	@previousRows 	int, 
		@rowsReviewed 	int, 
		@sql 			varchar(2000) 
SET @rowsReviewed = @RecsPerPage * @page 
SET @previousRows = @rowsReviewed - @RecsPerPage 
IF (@rowsReviewed = @RecsPerPage) 
	-- First page is requested 
BEGIN 
	SELECT @sql = ''
	SELECT @sql = @sql + ' select top ' + CONVERT(varchar, @RecsPerPage) + ' ' + @Fields 
	SELECT @sql = @sql + ' from ' + @Table 
	SELECT @sql = @sql + ' ORDER BY ' + @Key + ' asc ' 
	EXEC(@sql) 
END 
ELSE 
BEGIN 
	SELECT @sql = ''
	SELECT @sql = @sql + ' select top ' + CONVERT(varchar, @RecsPerPage) + ' ' + @Fields 
	SELECT @sql = @sql + ' from ' + @Table 
	SELECT @sql = @sql + ' where ' + @Key + ' in ' 
	SELECT @sql = @sql + ' 				(select top ' + CONVERT(varchar, @rowsReviewed) + @Key 
	SELECT @sql = @sql + '				from ' + @Table 
	SELECT @sql = @sql + '				 ORDER BY ' + @Key + ' asc )' 
	SELECT @sql = @sql + 'AND ' + @Key + ' not in ' 
	SELECT @sql = @sql + ' 				(select top ' + CONVERT(varchar, @previousRows) + @Key
	SELECT @sql = @sql + '				from ' + @Table 
	SELECT @sql = @sql + '				 ORDER BY ' + @Key + ' asc )' 
	SELECT @sql = @sql + ' ORDER BY ' + @Key + ' asc ' 
	EXEC(@sql) 
END
Go


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
7/6/2003 10:11:24 PM

hey folks ..
i found this code very useful..
thanx ... anyway for code..
(If this comment was disrespectful, please report it.)

 
9/4/2006 7:48:18 AMDeepak

was very helpful, good work
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.