获取列表存储过程

获取列表存储过程如下:

ALTER PROCEDURE [dbo].[ds_TopListWeek]
    @keyword varchar(30),   -- 区分活动
    @userName varchar(50),  -- 作者
    @title varchar(100),    -- 标题 
    @time varchar(10),      -- 时间
    @pageindex int,         -- 当前页码
    @pagesize int           -- 每页记录数
AS
BEGIN
    declare @frmindex int,@endindex int
    set @frmindex=(@pageindex-1)*@pagesize
    set @endindex=@pageindex*@pagesize

    declare @sql varchar(3000),@condition varchar(1000),@tempdate datetime,@likeUserName varchar(50),@likeTitle varchar(100)
    --查询时间的周的周一时间
    set @tempdate = dateadd(day,1 - (datepart(weekday,@time) + @@datefirst - 2) % 7 - 1,@time)
    set @condition=' where t.keyword=''' + @keyword + ''' and t.top_type=0'

    if (@userName!='' and @userName is not null)
    begin
        set @likeUserName = dbo.f_Filter_EscapeStr(@userName)
        set @condition=@condition+' and t.nickname like ''%'+@likeUserName+'%'' escape ''/'''
    end
    if (@title!='' and @title is not null)
    begin
        set @likeTitle = dbo.f_Filter_EscapeStr(@title)
        set @condition=@condition+' and n.title like ''%'+@likeTitle+'%'' escape ''/'''
    end

    set @sql = 'with tb as('
            +'select t.t_id,t.news_id,t.username,t.nickname,t.praisecnt,t.randomcnt,t.praisecnt+t.randomcnt as cnt,t.create_time,t.IsPub,n.title,n.url'
            +' from ds_toplist t inner join sy_news n on t.news_id=n.news_id'
            + @condition
            +' and t.create_time between ''' + convert(varchar(20), @tempdate, 120) +''' and dateadd(day,6,'''+ convert(varchar(20), @tempdate, 23) + ''')'
            +'),tbNoRepeat as ('
            +'select t.* from (select distinct nickname from tb) t1'
            +' cross apply(select top 1 * from tb t2 where t1.nickname=t2.nickname order by t2.cnt desc) t'
            +'),tbRes as('
            +'select t.t_id,t.news_id,t.username,t.nickname,t.praisecnt,t.randomcnt,t.title,'
            +'t.url,t.IsPub,t.create_time,row_number() over(order by (t.praisecnt + t.randomcnt) desc) as place from tbNoRepeat t)'
            +'select *,(select count(1) from tbRes) as pagecount from tbRes'
            +' where place > ' + cast(@frmindex as varchar) +' and place <='+ cast(@endindex as varchar)
    print @sql
    exec (@sql)
END
原文地址:https://www.cnblogs.com/wxh19860528/p/3412636.html