标签:ng2 总结 returns mit summary from query hand dex
以下演示样例总结了,SqlServer数据库 经常使用分页方法,仅供学习參考
/********** 使用 RowNumber 和 Between And 组合分页 **********/ CREATE PROC proc_FuzzySearchAndPaging @pageIndex int, --页索引 @pageSize int, --页大小 @SearchKey Nvarchar(10), --查询keyword @TotalCount int OutPut --总数据条数 AS BEGIN --查询 当前页 数据 SELECT * FROM( SELECT *,[NO]=ROW_NUMBER() OVER(ORDER BY s.s_id DESC) FROM StuInfo s WHERE s.s_name Like(‘%‘+@SearchKey+‘%‘) ) t WHERE t.[NO] BETWEEN @pageSize*(@pageIndex-1)+1 AND @pageIndex*@pageSize ORDER BY t.s_id DESC --总数据条数 SELECT @TotalCount = COUNT(*) FROM StuInfo s WHERE s.s_name Like(‘%‘+@SearchKey+‘%‘) END GO
/********** 使用 TOP 和 NOT IN 组合分页 **********/ CREATE PROC proc_FuzzySearchAndPaging2 @PageIndex int, --当前页索引 @PageSize int, --每页显示的数据条数 @FuzzyKey Nvarchar(20), --模糊匹配的keyword @Count int OUTPUT --总数据条数(用来推断要分多少页) AS BEGIN SELECT TOP(@PageSize) * FROM StuInfo s WHERE s.s_name LIKE(‘%‘+@FuzzyKey+‘%‘) AND s.s_id NOT IN( SELECT TOP((@PageIndex-1)*@PageSize) s.s_id FROM StuInfo s WHERE s.s_name LIKE(‘%‘+@FuzzyKey+‘%‘) ORDER BY s.s_id DESC ) ORDER BY s.s_id DESC --总数据条数 SELECT @Count=COUNT(*) FROM StuInfo s WHERE s.s_name LIKE(‘%‘+@FuzzyKey+‘%‘) END GO
/// <summary> /// 分页 /// </summary> /// <param name="key">查询keyword</param> /// <param name="pageIndex">页索引</param> /// <param name="pageSize">页大小</param> /// <param name="pageCount">总页数</param> /// <returns></returns> public IList<EA_Script> FuzzyPaging(String key, int pageIndex, int pageSize, ref int pageCount) { var query = from e in dc.EA_Script where e.FName.Contains(key) orderby e.Id descending select e; //总页数 pageCount = query.Count() % pageSize == 0 ? (query.Count() % pageSize) : query.Count() / pageSize + 1; return query.Skip(pageIndex - 1).Take(pageSize).ToList(); }
如有,更好方法欢迎拿出来分享!
扩充:分页时,能够充分借助 暂时表 和 WITH AS 语句提高查询效率
DECLARE @SearchKey Nvarchar(10) --查询keyword WITH t AS( SELECT * FROM StuInfo s WHERE s.s_name Like(‘%‘+@SearchKey+‘%‘) )
DECLARE @SearchKey Nvarchar(10) --查询keyword SELECT * INTO #temp2 FROM ( SELECT * FROM StuInfo s WHERE s.s_name Like(‘%‘+@SearchKey+‘%‘) ) u
SELECT u.* FROM users AS u LIMIT ($pIndex-1)*$pSize,$pIndex*$pSize;
标签:ng2 总结 returns mit summary from query hand dex
原文地址:http://www.cnblogs.com/brucemengbm/p/6820463.html