分页方案

news/2024/7/3 19:43:06

分页方案

 

下面的存储过程不仅含有分页方案,还会根据页面传来的参数来确定是否进行数据总数统计。

-- 获取指定页的数据

Create PROCEDURE pagination3

@tblName   varchar(255),       -- 表名

@strGetFields varchar(1000) = '*',  -- 需要返回的列

@fldName varchar(255)='',      -- 排序的字段名

@PageSize   int = 10,          -- 页尺寸

@PageIndex  int = 1,           -- 页码

@doCount  bit = 0,   -- 返回记录总数, 非 0 值则返回

@OrderType bit = 0,  -- 设置排序类型, 非 0 值则降序

@strWhere  varchar(1500) = ''  -- 查询条件 (注意: 不要加 where)

AS

declare @strSQL   varchar(5000)       -- 主语句

declare @strTmp   varchar(110)        -- 临时变量

declare @strOrder varchar(400)        -- 排序类型

 

if @doCount != 0

  begin

    if @strWhere !=''

    set @strSQL = "select count(*) as Total from [" + @tblName + "] where "+@strWhere

    else

    set @strSQL = "select count(*) as Total from [" + @tblName + "]"

end 

--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况

else

begin

 

if @OrderType != 0

begin

    set @strTmp = "<(select min"

set @strOrder = " order by [" + @fldName +"] desc"

--如果@OrderType不是0,就执行降序,这句很重要!

end

else

begin

    set @strTmp = ">(select max"

    set @strOrder = " order by [" + @fldName +"] asc"

end

 

if @PageIndex = 1

begin

    if @strWhere != ''  

    set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ "  from [" + @tblName + "] where " + @strWhere + " " + @strOrder

     else

     set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ "  from ["+ @tblName + "] "+ @strOrder

--如果是第一页就执行以上代码,这样会加快执行速度

end

else

begin

--以下代码赋予了@strSQL以真正执行的SQL代码

set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ "  from ["

    + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["+ @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"+ @strOrder

 

if @strWhere != ''

    set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ "  from ["

        + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["

        + @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["

        + @fldName + "] from [" + @tblName + "] where " + @strWhere + " "

        + @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder

end

end  

exec (@strSQL)

GO 


http://www.niftyadmin.cn/n/4364467.html

相关文章

cocos2d-x动画制作(cocos2d-x2.1)

本博客参考资料&#xff1a;http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d 预备 用到的图片下载 TexturePacker是资源打包器&#xff0c;将资源打包&#xff0c;一起载入到游戏既方便又可以提升性能。 将下载好的图片解压缩&#…

用户控件使用一例

用户控件使用一例 用户控件的简单使用例子&#xff0c;做一个用户登陆的用户控件。用户登陆某个系统是个非常常见的问题&#xff0c;这里笔者想把这样的问题作为一个用户控件来使用&#xff0c;方便各个用户。 1、在工程中新建立一个Web用户控件&#xff0c;此时加入的名称为F…

cocos2d-x按钮菜单(cocos2d-x2.1)

首先看看程序运行时&#xff1a; 程序中有四种菜单项按钮&#xff1a;文本菜单项&#xff08;stop walk&#xff09;&#xff0c;图字菜单项&#xff08;Hide Bear&#xff09;&#xff0c;Toggle菜单项&#xff08;Go Right&#xff09;以及图片菜单&#xff08;程序开关&…

用下拉列表控制gridview的分页

用下拉列表控制gridview的分页 protected void GridView1_DataBound(object sender, EventArgs e) { if (!IsPostBack) {//判断页是否第一次载入&#xff0c;如果你想知道为什么&#xff0c;恰恰我又不想解释&#xff0c;那你去掉这个试下即可 …

cocos2d-x GUI控件的使用(cocos2d-x2.1)

在使用extensions时都需要额外指定include、lib&#xff0c;例如笔者的VS11环境&#xff0c;右键项目 属性-->配置属性-->VC目录在包含目录中添加F:\cocos2d-2.1beta3-x-2.1.1\extensions;以及在链接器-->命令行添加libextensions.lib或者使用代码添加 #pragma comme…

用正则表达式突出显示字符串中查询到的单词的函数

用正则表达式突出显示字符串中查询到的单词的函数 Function BoldWord(strContent,word)dim objRegExpSet objRegExpnew RegExpobjRegExp.IgnoreCase trueobjRegExp.GlobalTrue objRegExp.Pattern"(" & word & ")"strContentobjRegExp.Replace(st…

box2d弹球 cocos2d-x重力感应(cocos2d-x2.1)

本博客例子参考&#xff1a;raywenderlich 我将上面obj-c的翻译成了C&#xff0c;改动很少。首先在win32下新建新工程 修改HelloWorldScene.h #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "Box2D/Box2D.h&qu…

自定义控件的拖动

自定义控件的拖动 //添加事件 this.MouseUp new System.Windows.Forms.MouseEventHandler(this.DragEnd); this.MouseMove new System.Windows.Forms.MouseEventHandler(this.DragMove); this.MouseDown new System.Windows.Forms.MouseEventHandler(this.DragBegin);  …