zoukankan      html  css  js  c++  java
  • asp.net利用存储过程分页代码

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go

    --最通用的分页存储过程
    -- 获取指定页的数据 
     
    ALTER PROCEDURE [dbo].[Pagination] 
     
    @tblName   varchar(255),       -- 表名 
     
    @strGetFields varchar(1000) = '*',  -- 需要返回的列 
     
    @fldName varchar(255)='',      -- 排序的字段名 
     
    @PageSize   int = 5,          -- 页尺寸 
     
    @PageIndex  int = 1,           -- 页码 
     
    @doCount  int = 1,   -- 返回记录总数, 非 0 值则返回 
     
    @OrderType int = 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 !=''  --有where条件
     
       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 != ''    --有where条件
     
        set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '  from ['+ @tblName +'] where ' + @strWhere + ' '  
     
        else  --没有where条件
     
        set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '  from ['+ @tblName +'] '
     
     if @fldName!='' --判断排序字段是否为空
      set @strSQL=@strSQL+@strOrder

    --如果是第一页就执行以上代码,这样会加快执行速度 
     
    end 
     
    else  --不是第一页情况下
     
    begin 
     
    --以下代码赋予了@strSQL以真正执行的SQL代码 
     
     
    if @strWhere != '' --有where条件情况下
     
     begin
     if @fldName!='' --判断排序字段是否为空
      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 
     else
      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 + ' ) as tblTmp) and ' + @strWhere
     end

    else--没有where条件情况下

     begin
     if @fldName!='' --判断排序字段是否为空
      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
     else
      set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '  from [' + @tblName +'] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) 
      from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + '] from ['+ @tblName +']) as tblTmp)' 
     end

    end 
     
    end
    exec ( @strSQL)




    http://blog.csdn.net/Xgz_Lzg/archive/2007/08/23/1755869.aspx

  • 相关阅读:
    IIS7配置URL Rewrite链接重写
    wordpress导航菜单的链接支持弹出新页面
    c++绝对是拯救了世界,特别是程序员
    Linux 磁盘坏道检测和修复
    centos里mysql无法用localhost连接的解决方法
    php扩展开发
    IP多播
    因特网的路由选择协议
    ICMP协议
    ARP协议
  • 原文地址:https://www.cnblogs.com/hyd309/p/1540594.html
Copyright © 2011-2022 走看看