zoukankan      html  css  js  c++  java
  • 【SQL 代码】Sql分页(自用)

    效果图:

    下面是存储过程的创建,用的时候调用就行了

    /****** Object:  StoredProcedure [dbo].[spSqlPageByRownumber]    Script Date: 2015/3/5 17:34:38 ******/
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE proc [dbo].[spSqlPageByRownumber]
    @tbName varchar(255),        --表名
    @tbFields varchar(1000),      --返回字段
    @PageSize int,                --页尺寸
    @PageIndex int,                --页码
    @strWhere varchar(1000),    --查询条件
    @StrOrder varchar(255)  --排序条件
    as
    declare @strSql varchar(5000)    --主语句
    declare @strSqlCount nvarchar(500)--
    --------------总记录数---------------
    if @strWhere !=''
    begin
    set @strSqlCount='Select count(*) as TotalCout from  ' + @tbName + ' where '+ @strWhere
    end
    else
    begin
    set @strSqlCount='Select count(*) as TotalCout from  ' + @tbName
    end
    --------------分页------------
    if @PageIndex <= 0
    begin
      set @PageIndex = 1
    end
    if @strWhere !=''
    begin
    set @strSql='Select * from (Select  row_number() over('+@strOrder+') rowId,'+ @tbFields
    +' from ' + @tbName + ' where  ' + @strWhere+' ) tb where tb.rowId >'+str((@PageIndex-1)*@PageSize)
    +' and tb.rowId <= ' +str(@PageIndex*@PageSize)
    end
    else
    begin
    set @strSql='Select * from (Select  row_number() over('+@strOrder+') rowId,'+ @tbFields
    +' from ' + @tbName + ' ) tb where tb.rowId >'+str((@PageIndex-1)*@PageSize)
    +' and tb.rowId <= ' +str(@PageIndex*@PageSize)
    end
    
    exec(@strSqlCount)
    exec(@strSql)
    GO

     执行:

    exec [dbo].spSqlPageByRownumber 'GoodsCategory','*',10,2,'Cid > 0','order by Cid desc'
  • 相关阅读:
    Codeforces 878A
    Codeforces 873B-Balanced Substring
    codeforces 868C
    51nod 1402 最大值(贪心)
    最小正子段和 贪心
    codeforces 819B
    Codeforces 785D
    Codeforces 864E
    863D
    UVA 1380 A Scheduling Problem
  • 原文地址:https://www.cnblogs.com/yc-755909659/p/4001712.html
Copyright © 2011-2022 走看看