求一分页存储过程!数据上万!也可能上百万千万!要求尽量减少服务器负荷!不能用游标!游标访问太慢!就这么点分了!!!!解决马上揭贴!!!
protected int PerPage = 10;int newTop = PerPage * (int)ViewState["CurrentPageNumber"];
int oldTop = newTop - 10;string str = "select * from (select top "+newTop+" * from T_Balcony_DreamInfo order by PK_ID) a where a. PK_ID not in(select top "+oldTop+" PK_ID from T_Balcony_DreamInfo) order by PK_ID";select count(PK_ID) from T_Balcony_DreamInfo上面是我自己做的一个分页SQL~~但是效果不怎么好!!!
protected int PerPage = 10;int newTop = PerPage * (int)ViewState["CurrentPageNumber"];
int oldTop = newTop - 10;string str = "select * from (select top "+newTop+" * from T_Balcony_DreamInfo order by PK_ID) a where a. PK_ID not in(select top "+oldTop+" PK_ID from T_Balcony_DreamInfo) order by PK_ID";select count(PK_ID) from T_Balcony_DreamInfo上面是我自己做的一个分页SQL~~但是效果不怎么好!!!
drop procedure [dbo].[GetPagingData]
GOSET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
--exec dbo.GetPagingData ' Select * From dbo.WebItemDetail','序号',5,2,'Desc','*','1=1',' ORDER BY 序号 desc'
GO
create proc dbo.GetPagingData(
@TBName nvarchar(2000)='',--表名,视图,SQL
@KeyField nvarchar(100)='ID',--关键字段名,默认为 ID,该字段要求是表中的索引 或 无重复和不为空的字段
@PageSize int=10,--每页的记录数,默认为 10
@CurPage int=1,--表示当前页 1
@KeyAscDesc nvarchar(4)='ASC',--关键字的升、降序,默认为升序 ASC , 降序为 DESC
@Fields nvarchar(2000)='*',--所选择的列名,默认为全选
@Condition nvarchar(2000)='1=1',--where 条件,默认为空
@Order nvarchar(200)=''--排序条件,默认为空
)ASif @TBName = ''
begin
raiserror('请指定表名!',11,1)
return
end
ELSE
BEGIN
IF(PATINDEX('%from%',@TBName)>0)
BEGIN
SET @TBName = ' (' + @TBName + ') tempTable '
END
END
if @PageSize <=0 or @CurPage <0
begin
raiserror('当前页数和每页的记录数都必须大于零!',11,1)
return
end
if @KeyAscDesc = 'DESC'
set @KeyAscDesc = '<'
else
set @KeyAscDesc = '>'
if @Condition <> ''
set @Condition = ' where ' + @Condition
declare @SQL nvarchar(2000)set @SQL = ''
if @CurPage = 1
set @SQL = @SQL + 'SELECT Top ' + cast(@PageSize as nvarchar(20)) + ' ' + @Fields + ' FROM ' + @TBName + @Condition + ' ' + @Order
else
begin
declare @iTopNum int
set @iTopNum = @PageSize * (@CurPage - 1)
set @SQL = @SQL + 'declare @sLastValue nvarchar(100)' + char(13)
set @SQL = @SQL + 'SELECT Top ' + cast(@iTopNum as nvarchar(20)) + ' @sLastValue=' + @KeyField + ' FROM ' + @TBName + @Condition + ' ' + @Order + char(13)declare @Condition2 nvarchar(200)
if @Condition = ''
set @Condition2 = ' where ' + @KeyField + @KeyAscDesc + '@sLastValue '
else
set @Condition2 = ' and ' + @KeyField + @KeyAscDesc + '@sLastValue '
set @SQL = @SQL + 'SELECT Top ' + cast(@PageSize as nvarchar(20)) + ' ' + @Fields + ' FROM ' + @TBName + @Condition + @Condition2 + @Order
end
EXECUTE sp_executesql @SQL
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO=================
送你一个,你改改吧,别人的东西到你这未必就有多好使