读书人

求分页存储过程大家分享下多谢

发布时间: 2012-02-16 21:30:36 作者: rapoo

求分页存储过程,大家分享下,谢谢!
求一个现在大家用得比较多的分页存储过程!

[解决办法]
http://topic.csdn.net/u/20081227/21/cef88b01-6313-4f55-a0ab-60767cc8f18b.html
http://topic.csdn.net/u/20090323/17/b4147061-2353-4b4d-8766-ef9eeea5a661.html
[解决办法]
我的项目中用到的一个。

根据标题搜索。分页。

SQL code
if exists(select * from sysobjects where name = 'sp_bdocnotice_list')drop procedure sp_bdocnotice_listgocreate procedure sp_bdocnotice_list(@startIndex int,@endIndex int,@title varchar(50))asbegin with temptbl  as(    select  row_number() over ( order by createtime desc) as row,    * from bdocnotice     where state  = 0 and ntitle like'%'+@title+'%')select * from temptbl  where row between @startIndex and  @endIndex end
[解决办法]
CREATE PROCEDURE GetProductsInCategory
(
@categoryID int,
@pageIndex int,--当前页面
@pageSize int,--每页记录数
@descriptionLength int,--商品描述字段的长度
@recordCount int output--总的记录数
)
AS
SELECT * FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY Product.productID) rowNumber,
Product.productID,productName,
description=
case
when len(Product.description)>@descriptionLength then substring(Product.description,1,@descriptionLength) + '...'
else Product.description
end,
price,image1FileName,image2FileName,
onDepartmentPromotion,onCatalogPromotion,discount,author,publisherName,publishDate
FROM Product INNER JOIN ProductCategory
ON Product.productID=ProductCategory.productID
INNER JOIN Publisher
ON Publisher.publisherId = Product.publisherId
WHERE ProductCategory.categoryID=@categoryID
) Temp
WHERE rowNumber > (@pageIndex-1)*@pageSize AND rowNumber<=@pageIndex*@pageSize

SELECT @recordCount=count(Product.productId)
FROM Product INNER JOIN ProductCategory ON Product.productID=ProductCategory.productID
INNER JOIN Publisher
ON Publisher.publisherId = Product.publisherId
WHERE ProductCategory.categoryID=@categoryID
GO
[解决办法]
create proc [dbo].[pyESPager2008]
@CurrentPage int,
@PageSize int,
@TableName varchar(8000),
@Columns nvarchar(4000),
@Conditions nvarchar(4000),
@SortColumns nvarchar(1000),
@SortType nvarchar(4)
AS
set nocount on
DECLARE @Sql NVARCHAR(MAX),@RecordCount int,@PageCount int,@StartId int
SET @Sql='SELECT @RecordCount = COUNT(*) FROM ' +@TableName+' WHERE (1=1) '+@Conditions

EXECUTE sp_executesql @Sql,N'@RecordCount int OUTPUT',@RecordCount OUTPUT


SET @PageCount= (@RecordCount - 1) / @PageSize + 1
IF @PageCount=0
SET @PageCount=1
IF @CurrentPage >@PageCount
SET @CurrentPage =@PageCount
IF @CurrentPage < 1
SET @CurrentPage = 1
SET @StartId = (@CurrentPage -1) * @PageSize + 1

SET ROWCOUNT @PageSize
SET @Sql='SELECT ' + @Columns + ' FROM (SELECT ROW_NUMBER() OVER(ORDER BY '+@SortColumns+' ' + @SortType+') as rowNum, '+ @Columns+ '
FROM '+ @TableName+ ' WHERE (1=1) '+@Conditions+') as t WHERE rowNum >= '+CAST(@StartId as varchar(32))+@Conditions+' ORDER BY '+@SortColumns+' '+@SortType;
EXEC(@Sql)
SELECT @RecordCount AS RecordCount,@CurrentPage AS CurrentPage,@PageCount AS PageCount,@PageSize as CountPerPage

我们现在就用这个...推荐

读书人网 >VB

热点推荐