改下简单的存储过程分页
ALTER procedure [dbo].[osmPage]
(@startIndex int,
@endIndex int)
as
begin
with temptbl as (
SELECT ROW_NUMBER() OVER (ORDER BY salaDate desc)AS Row, * from product )
SELECT * FROM temptbl where Row between @startIndex and @endIndex
end
我用的aspnetpager自动生成的简单存储过程.
如果我输入显示记录数5,索引页1,显示5个数据,是正常的,然后我跳转第二页,就会变成5,2 查出来的记录就只有4个,往后就递减。
请帮我改下存储过程。。。谢谢
--------------------编程问答--------------------
ALTER PROCEDURE [dbo].[osmPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 主键字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(7000) -- 主语句
declare @strTmp varchar(1000) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
set @strSQL = 'select top ' + cast(@PageSize as varchar(10)) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + cast((@PageIndex-1)*@PageSize as varchar(10)) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + cast(@PageSize as varchar(10)) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + cast((@PageIndex-1)*@PageSize as varchar(10)) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp =''
if @strWhere != ''
set @strTmp = ' where ' + @strWhere
set @strSQL = 'select top ' + cast(@PageSize as varchar(10)) + ' * from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
if(@IsReCount<>0)
begin
set @strSQL = 'select count(1) as Total from [' + @tblName + ']'
if @strWhere != ''
set @strSQL = 'select count(1) as Total from [' + @tblName + ']'+' where ' + @strWhere
end
EXEC (@strSQL)
return
用这个吧。虽然比较老,还是挺管用的。 --------------------编程问答-------------------- @startIndex and @endIndex
这两个参数的问题 --------------------编程问答-------------------- pageIndex,pageSize
应该用这两个来控件,不应该用startIndex和endIndex
@sql='Select top '+ str(@pageSize) +' * From (Select *,ROW_NUMBER() OVER(ORDER BY [排序,也可以做为参数传进来]) as RowNum From [tableName]) as newTable Where (RowNum > '+ str((@pageindex - 1) * @pagesize) +')'
Exec @sql --------------------编程问答-------------------- 可以不用存储过程,直接写sql语句还简单得多
public DataSet GetinfoList(string table, int pageindex, int pagesize, string orderFid, string Conditions, string[] strparam, object[] strval)
{
string query = "Select top {0} * From "
+ "(Select *,ROW_NUMBER() OVER(ORDER BY {2}) as RowNum From {4} {3}) as newTable "
+ "Where (RowNum > {1} )";
query = string.Format(query, pagesize, (pageindex - 1) * pagesize, orderFid, Conditions, table);
//string query = "Select top {0} * From {1} {2} and id not in(Select top {3} id From {1} {2} Order by {4}) Order by {4}";
//query = string.Format(query, rowcount, table, Conditions, (rowpage - 1) * rowcount + 1, orderFid);
return db.GetDataSet(query, strparam, strval);
}
补充:.NET技术 , ASP.NET