T @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ('
+ 'SELECT TOP ' + STR(@PageSize) + ' '
+ @FieldList + ' FROM '
+ @TableName + @new_where2 + @PrimaryKey + ' < '
+ '(SELECT MIN(' + @PrimaryKey + ') FROM (SELECT TOP '
+ STR(@TotalCount-@PageSize*@PageIndex) + ' ' + @PrimaryKey
+ ' FROM ' + @TableName
+ @new_where1 + @new_order2 +' ) AS TMP) '+ @new_order2
+ ' ) AS TMP ' + @new_order1
END
END
IF @SortType = 2 --仅主键反序排序
BEGIN
IF @PageIndex <= CEILING((@TotalCount+0.0)/@PageSize)/2 --正向检索
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
+ @TableName + @new_where2 + @PrimaryKey + ' < '
+ '(SELECT MIN(' + @PrimaryKey + ') FROM (SELECT TOP '
+ STR(@PageSize*(@PageIndex-1)) + ' ' + @PrimaryKey
+' FROM '+ @TableName
+ @new_where1 + @new_order1 + ') AS TMP) '+ @new_order1
END
ELSE --反向检索
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ('
+ 'SELECT TOP ' + STR(@PageSize) + ' '
+ @FieldList + ' FROM '
+ @TableName + @new_where2 + @PrimaryKey + ' > '
+ '(SELECT MAX(' + @PrimaryKey + ') FROM (SELECT TOP '
+ STR(@TotalCount-@PageSize*@PageIndex) + ' ' + @PrimaryKey
+ ' FROM ' + @TableName
+ @new_where1 + @new_order2 +' ) AS TMP) '+ @new_order2
+ ' ) AS TMP ' + @new_order1
END
END
IF @SortType = 3 --多列排序,必须包含主键,且放置最后,否则不处理
BEGIN
IF CHARINDEX(',' + @PrimaryKey + ' ',',' + @Order) = 0
BEGIN PRINT('ERR_02') RETURN END
IF @PageIndex <= CEILING((@TotalCount+0.0)/@PageSize)/2 --正向检索
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '
+ 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '
+ ' SELECT TOP ' + STR(@PageSize*@PageIndex) + ' ' + @FieldList
+ ' FROM ' + @TableName + @new_where1 + @new_order1 + ' ) AS TMP '
+ @new_order2 + ' ) AS TMP ' + @new_order1
END
ELSE --反向检索
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '
+ 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '
+ ' SELECT TOP ' + STR(@PageSize*@PageIndex) + ' ' + @FieldList
+ ' FROM ' + @TableName + @new_where1 + @new_order1 + ' ) AS TMP '
+ @new_order2 + ' ) AS TMP ' + @new_order1
END
END
END
EXEC(@Sql)
调用方法:
///
/// 存储过程分页
///
///
///
///
///
///
///
///
public DataTable GetLineRoadPage(int pageIndex, int pageSize, string where, string order, int sortType, out int totalCount)
{
totalCount = 0;
SqlParameter[] parameters = {
new SqlParameter("@TableName", SqlDbType.VarChar,200),
new SqlParameter("@FieldList", SqlDbType.VarChar,2000),
new SqlParameter("@PrimaryKey", SqlDbType.VarChar,100),
new SqlParameter("@Where", SqlDbType.VarChar,2000),
new SqlParameter("@Order", SqlDbType.VarChar,1000),
new SqlParameter("@SortType", SqlDbType.Int,4),
new SqlParameter("@RecorderCount", SqlDbType.Int,4),
new SqlParameter("@PageSize", SqlDbType.Int,4),
new SqlParameter("@PageIndex", SqlDbType.Int,4),
new SqlParameter("@TotalCount", SqlDbType.Int,4),
new SqlParameter("@TotalPageCount", SqlDbType.Int,4)};
parameters[0].Value = "vLineRoad";
parameters[1].Value = "[LrID],[Title],[SettlePrice],[MarketPrice],[ImageUrls],[LineType],[TraficTool],[NeedDays],[IsShow],[IsRecommend],[ThemID],[SplID],[LinePriceType],[IsUseTicket],[NeedNights],[ProductCode],[ChildrenPrice],[CreateTime],[UpdateTime],[SpLnName],[SupplierName],[Tel],[Dates]";
parameters[2].Value = "LrID";
parameters[3].Value = where;
parameters[4].Value = order;
parameters[5].Value = sortType;
parameters[6].Value = 0;
parameters[7].Value = pageSize;
parameters[8].Value = pageIndex;
parameters[9].Direction = ParameterDirection.Output;
parameters[10].Direction = ParameterDirection.Output;
DataTable dt = DbHelperSQL.RunProcedureToDataTable("P_ViewPage", parameters);
totalCount = Convert.ToInt32(parameters[9].Value);
return dt;
}