日期:2010-01-05  浏览次数:20841 次

<%
'    /*智能返回分页SQL语句*/
'    /// <summary>
'    /// 功能:智能返回分页SQL语句
'    /// </summary>
'    /// <param name="primaryKey">主键(不能为空)</param>
'    /// <param name="queryFields">提取字段(不能为空)</param>
'    /// <param name="tableName">表(理论上允许多表)</param>
'    /// <param name="condition">条件(可以空)</param>
'    /// <param name="OrderBy">排序,格式:字段名+""+ASC(可以空)</param>
'    /// <param name="pageSize">分页数(不能为空)</param>
'    /// <param name="pageIndex">当前页,起始为:1(不能为空)</param>
'    /// <returns></returns>
Public Function GetPageListSql( primaryKey,  queryFields,  tableName,  condition,  orderBy,  pageSize,  pageIndex)

Dim strTmp,SqlSelect,SqlPrimaryKeySelect,strOrderBy,strWhere,strTop,pageindexsize
 strTmp=""
'//---strTmp用于返回的SQL语句
 SqlSelect = ""
 SqlPrimaryKeySelect = ""
 strOrderBy = ""
 strWhere = " where 1=1 "
 strTop = ""
 pageindexsize = 0

'        //0:分页数量
'        //1:提取字段
'        //2:表
'        //3:条件
'        //4:主键不存在的记录
'        //5:排序
        SqlSelect = " select top {0} {1} from {2} {3} {4} {5}"
 '       //0:主键
 '       //1:TOP数量,为分页数*(排序号-1)
 '       //2:表
 '       //3:条件
 '       //4:排序
        SqlPrimaryKeySelect = " and {0} not in (select {1} {0} from {2} {3} {4}) "

        if orderBy <> "" then
            strOrderBy = " order by "&orderBy
           End if
     if condition <> "" then
            strWhere =strWhere&" and "&condition
         pageindexsize = (pageIndex - 1) * pageSize
          End if
      if cint(pageindexsize) > 0 then
      
            strTop = " top " & pageindexsize

            SqlPrimaryKeySelect = Replace(Replace(Replace(Replace(Replace(SqlPrimaryKeySelect,"{0}", primaryKey),"{1}", strTop),"{2}", tableName),"{3}", strWhere),"{4}", strOrderBy)

            strTmp = Replace(Replace(Replace(Replace(Replace(Replace(SqlSelect,"{0}", pageSize),"{1}", queryFields),"{2}", tableName),"{3}", strWhere),"{4}", SqlPrimaryKeySelect),"{5}", strOrderBy)

      
        else
            strT