zoukankan      html  css  js  c++  java
  • sql 存储过程—分页获取信息

    if exists(select * from sysobjects 
                where id = object_id(N'up_GetMusicByCondition') 
                and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure up_GetMusicByCondition
    GO
    --分页存储过程
    create procedure up_GetMusicByCondition
    (
        @Condition nvarchar(1000), --查询条件
        @PageSize int,               --每页显示多少条
        @CurrentPage int,          --当前显示第几页
        @Pages int output          --分页后的总页数
    )
    as
    begin
        declare @sql nvarchar(2000)--声明一个字符串变量
        declare @total int         --辅助变量,保存按当前条件总共有多少条记录
        --给@Pages赋值
        set @sql='select @temp=COUNT(*) from [Music],[Country] 
            where [Music].[CountryId]=[Country].[CountryId] and '+@Condition
        exec sp_executesql  @sql,N'@temp int output',@total output
        --求出总页数
        set @Pages=ceiling(CONVERT(decimal(6,2),@total)/@PageSize)
        --查询目标页数据
        declare @begin int  --要找的数据的起始编码
        declare @end int    --要找的数据的结束编码
        set @begin=@PageSize*(@CurrentPage-1)+1
        set @end=@CurrentPage*@PageSize    
        set @sql='select * from (
            select row_number() over(order by Id) AS ''Num''
            ,[Music].*,[Country].[CountryName] 
            from [Music],[Country] 
            where [Music].[CountryId]=[Country].[CountryId] and '
            +@Condition+') T where T.Num between '
            +convert(varchar(5),@begin)
            +' and '
            +convert(varchar(5),@end)
        print @sql--可以不要是为了调试找错
        exec(@sql)
    end
  • 相关阅读:
    阿里PAI深度学习组件:Tensorflow实现图片智能分类实验
    IDEA with MaxCompute Stadio
    阿里云大数据产品解决方案ODPSADSSTERAMCOMPUTEPAI介绍
    Hive DDL ROW FORMAT
    Spark-Streaming和Kafka集成指南
    网站分析指标
    网站分析概要
    前端规范大总结
    不容错过的20段CSS代码
    利用CSS实现居中对齐
  • 原文地址:https://www.cnblogs.com/cylblogs/p/4943796.html
Copyright © 2011-2022 走看看