您现在的位置是:芭奇站群管理系统 > 优化技巧 > -> 分页存储过程

分页存储过程

时间:2010-05-19 22:48

  use[sxb_test_db]

  go

  /对象: storedprocedure[dbo].[uspt_testpaper_getlist_byall_request]   脚本日期:03/03/201016:15:07/

  setansi_nullson

  go

  setquoted_identifieron

  go

  --=============================================

  --author:      liujing

  --createdate:2009-11-27

  --description:   根据@keywordtypeid@subtypeid搜索

  --=============================================

  alterprocedure[dbo].[aaaaaaaaa]

  @pagesizeint,

  @pageindexint,

  @keywordvarchar(256),

  @typeidint,

  @subtypeidint,

  @paperidsvarchar(max)=null

  as

  begin

  setnocounton;

  ---------------------------------------------------------------------------

  -----begin分页临时变量不需要改动

  declare@pagecount  int            ----查询结果分页后的总页数

  declare@counts   int              ----查询到的记录数

  declare@tmpcountsint

  declare@pagedownint

  declare@pageupint

  -----end分页临时变量不需要改动

  ---------------------------------------------------------------------------

  ---------------------------------------------------------------------------

  -----begin组成分页条件

  declare@sqlwherenvarchar(max)

  declare@sqlnvarchar(max)

  declare@sqlcellnvarchar(max)---要查出哪些字段,所有字段用

  declare@orderbynvarchar(max)---排序条件组合,需要写orderby

  set@sqlwhere=where   [stuonly]=0

  and[stuonly]=0

  and[issell]=0

  and[ischeck]=1

  and[mark]=0

  if(@subtypeidisnotnull)

  set@sqlwhere=@sqlwhere+and[subtypeid]=@subtypeid

  elseif(@typeidisnotnull)

  set@sqlwhere=@sqlwhere+and[typeid]=@typeid

  if(@keywordisnotnull)

  set@sqlwhere=@sqlwhere+and([papername]like%+@keyword+%or[tag]like%+@keyword+%) 

  if(@paperidsisnotnull)

  set@sqlwhere=@sqlwhere+andcharindex(,+rtrim(papereid)+,,,+@paperids+,)=0 

  set@sql= select @counts=count([papereid]) 

  from[t_testpaper]+ @sqlwhere

  execsp_executesql@sql,n@countsintout,@keywordvarchar(256),@typeidint,@subtypeidint,@paperidsvarchar(max)=null,@countsout,@keyword,@typeid,@subtypeid,@paperids---查出总记录

  -----end组成分页条件

  ---------------------------------------------------------------------------

  ---------------------------------------------------------------------------

  -----begin计算分页 不需要改动

  if@counts=0

  set@tmpcounts=1

  else

  set@tmpcounts=@counts

  --取得分页总数

  set@pagecount=(@tmpcounts+@pagesize-1)/@pagesize

  ///////当前页大于总页数取最后一页/

  if@pageindex>@pagecount

  set@pageindex=@pagecount

  if@pageindex<1

  set@pageindex=1

  set@pagedown=(@pagesize(@pageindex-1)+1) --当前页的第一条记录rowid

  set@pageup=@pagesize@pageindex--当前页的最后一条记录rowid

  -----end查出总记录不需要改动

  ---------------------------------------------------------------------------

  ---------------------------------------------------------------------------

  -----begin要查的字段,及排序条件

  set@sqlcell=[papereid],

  [typeid],

  [subtypeid],

  [tag],

  [paperabout],

  [posttime],

  [papercosts],

  [papername],

  [papertype],

  [readcount],

  [ischeck]

  set@orderby=orderby[posttime]desc,[papereid]desc

  -----end要查的字段,及排序条件

  ---------------------------------------------------------------------------

  ---------------------------------------------------------------------------

  -----begin最终查出结果

  if(@pageindex=1)

  begin

  set@sql=selecttop+rtrim(@pagesize)+@sqlcell+from[t_testpaper]+@sqlwhere+@orderby

  end

  else

  begin

  set@sql=select,@countsas[recordcounts],@pagecountas[pagecount]

  from

  (

  select   +@sqlcell+,

  pagetablerowid=row_number()over(+@orderby+)

  from[t_testpaper]+ @sqlwhere+

  )asdtable

  wherepagetablerowidbetween@pagedownand@pageup

  end

  execsp_executesql@sql,

  n@countsint,@keywordvarchar(256),@typeidint,@subtypeidint,@pagedownint,@pageupint,@pagecountint,@paperidsvarchar(max)=null,

  @counts,@keyword,@typeid,@subtypeid,@pagedown,@pageup,@pagecount,@paperids

  -----end最终查出结果

  ---------------------------------------------------------------------------

  end