zoukankan      html  css  js  c++  java
  • sql的万能分页 单表很多表通用

    --declare @count int

    --exec [list_page]'level_Id,up_User,Placement_id,username2,w_username,w_zhituiSumMoney,w_datetime','[user],wallet','[user].username2=wallet.w_username and w_datetime between ''2013-11-8'' and ''2013-11-10''','w_datetime',1,@count output,5

    --select @count

    --declare @count int
    --exec [list_page]'level_Id,username2,l_yeji,r_yeji,w_username,w_zhituiSumMoney,w_datetime,w_ldSumMoney,w_pzSumMoney','[user],wallet','[user].username2=wallet.w_username ','level_Id desc',1,@count output,5
    --select @count

    ALTER PROCEDURE [dbo].[list_page]
    @SelectList VARCHAR(2000)=' * ', --欲选择字段列表
    @TableSource VARCHAR(1000), --表名或视图表
    @SearchCondition VARCHAR(max)='', --查询条件
    @OrderExpression VARCHAR(1000)='id', --排序表达式
    @PageIndex INT = 1, --页号,从0开始
    @Counts int = 1 output, ----查询到的记录数
    @PageSize INT = 10 --页尺寸
    --@pagecount int output ---总控几页
    AS
    BEGIN
    IF @SelectList IS NULL OR LTRIM(RTRIM(@SelectList)) = ''
    BEGIN
    SET @SelectList = '*'
    END
    PRINT @SelectList

    SET @SearchCondition = ISNULL(@SearchCondition,'')
    SET @SearchCondition = LTRIM(RTRIM(@SearchCondition))
    IF @SearchCondition <> ''
    BEGIN
    IF UPPER(SUBSTRING(@SearchCondition,1,5)) <> 'WHERE'
    BEGIN
    SET @SearchCondition = 'WHERE ' + @SearchCondition
    END
    END
    PRINT @SearchCondition

    SET @OrderExpression = ISNULL(@OrderExpression,'')
    SET @OrderExpression = LTRIM(RTRIM(@OrderExpression))
    IF @OrderExpression <> ''
    BEGIN
    IF UPPER(SUBSTRING(@OrderExpression,1,5)) <> 'WHERE'
    BEGIN
    SET @OrderExpression = 'ORDER BY ' + @OrderExpression
    END
    END
    PRINT @OrderExpression

    IF @PageIndex IS NULL OR @PageIndex < 1
    BEGIN
    SET @PageIndex = 1
    END
    PRINT @PageIndex
    IF @PageSize IS NULL OR @PageSize < 1
    BEGIN
    SET @PageSize = 10
    END
    PRINT @PageSize

    DECLARE @SqlQuery VARCHAR(4000)

    SET @SqlQuery='SELECT *,RowNumber
    FROM
    (SELECT ' + @SelectList + ',ROW_NUMBER() OVER( '+ @OrderExpression +') AS RowNumber
    FROM '+@TableSource+' '+ @SearchCondition +') AS RowNumberTableSource
    WHERE RowNumber BETWEEN ' + CAST(((@PageIndex - 1)* @PageSize+1) AS VARCHAR)
    + ' AND ' +
    CAST((@PageIndex * @PageSize) AS VARCHAR)
    -- ORDER BY ' + @OrderExpression
    PRINT @SqlQuery
    SET NOCOUNT ON
    EXECUTE(@SqlQuery)
    SET NOCOUNT OFF

    -- set @Counts= @@RowCount
    --此处@strTmp为取得查询结果数量的语句
    Declare @strTmp nvarchar(1000) ----存放取得查询结果总数的查询语句和动态生成的SQL语句
    --此处@strTmp为取得查询结果数量的语句
    Set @strTmp = 'select @Counts=Count(*) FROM '+@TableSource +' ' + @SearchCondition

    ----取得查询结果总数量-----
    Exec Sp_executesql @strTmp,N'@Counts int out ',@Counts Out
    --set @pagecount=Ceiling(@Counts*1.0/@PageSize*1.0) out


    print @strTmp
    END

    2  单表分页

    select @rowcount=count(*) from ztjl where z_username=@us set @pageCount=ceiling(@rowcount/@pagesize)
    select * from
    (select row_number() over(order by Id) as t ,* from ztjl where z_username=@us) m
    where t between (@pagesize*(@pageindex-1)+1)and (@pageindex*@pagesize)

  • 相关阅读:
    第二次结对作业
    第二次编程作业
    第一次编程作业
    第一篇随笔
    个人总结
    用例图设计
    第二次个人编程作业
    第一次个人编程作业
    第一次博客作业
    个人总结-以梦为马,砥砺前行
  • 原文地址:https://www.cnblogs.com/cdaq/p/3424735.html
Copyright © 2011-2022 走看看