【原创】对文章分类的数据提取

1、发表缘由:

  做一个类似于博客的文章系统,其中里面有一个文章模块,在后者是用标签来添加分类的新闻的,比如:操作指引(classId=1),活动动态(classId=2),博主文章(classId=3)等等,其中博主文章是博主发布的,操作指引,活动动态等,是在系统管理后台发布的,并且都有一个置顶和前台推荐的的功能,就是在数据库中有个字段标识是否推荐,现在首页要求取某个分类的前n条,其中m(n>m)条是按是否推荐字段(TopNum),剩下的简单的将数据表设计一下,表结构如下:

 

id为表的索引,o,tiele应该是title,写错了,是标题,内容,分类,推荐值,用户编号,增加时间,在首页的话可以将提取出来的信息显示出来,作为一个引导作用,将用户引导到子页面。又有一个页面对应的分类有20条数据,也是按照这种要求进行提取,所以就需要一个统一的方法对这个表的数据进行提取。

 

2、思路:

  有了需求,就得想个办法,由于之前写过一个分页的存储过程,想能不能将这个需求结合分页的存储过程加以整合呢,所以就将上表所需的东西按需求房子一张临时表中,将这个临时表作为分页存储过程的主表,做到完美结合,结合需求,将参数整出来,参数如下:

@classId nvarchar(10),--文章分类(eg:1,2,3,4..)
@topNum int,--选取topNum=?的置顶条数
@isTopNum nvarchar(100),--TopNum是置顶的值,TopNum=1(前台推荐),TopNum=2(置顶).....
@selectTopNum nvarchar(100),--总共获取多少条数

@TotalPage int output,--总页数
@RowsCount int output,--总条数
@PageSize int,--每页显示数据
@CurrentPage int--当前分页

因为这个存储过程是结合特定的表而写的,所以没有对所需数据字段做什么参数,需要什么数据直接在存储过程里面直接定义就行了,如果selectTopNum的值为0的话,后面的分页参数就起作用了,不然分页就不起作用,就直接提前前面几条数据,这样就做到了通用,呵呵!

3、代码:

  最后将代码发布一下,供园子里的朋友们参考,当然,我个人更希望得到园友们的指点,以便提高,代码如下:

代码
  1 --获取文章表(article)中获取某?条记录,条件为指定分类(classid),指定行为(isTop=?)的前topNum条,剩下的按增加时间排序
  2 CREATE PROCEDURE selectTopN 
  3 
  4 @classId nvarchar(10),--文章分类(eg:1,2,3,4..)
  5 @topNum int,--选取istop=?的置顶条数
  6 @isTopNum nvarchar(100),--isTop是置顶的值,isTop=1(前台推荐),isTop=2(置顶).....
  7 @selectTopNum nvarchar(100),--总共获取多少条数
  8 
  9 @TotalPage int output,
 10 @RowsCount int output,
 11 @PageSize int,
 12 @CurrentPage int
 13 As
 14 
 15 Begin
 16       --print @PageSize
 17      declare @RecordCount float
 18      declare @PageNum int --分页依据数
 19      Declare @TemSql nvarchar(1000)
 20      Declare @nRd int
 21      declare @afterRows int
 22      declare @tempTableName nvarchar(10)
 23 
 24      declare @sql nvarchar(200)
 25      declare @topClassNum nvarchar(100)
 26      declare @selectNum nvarchar(100)
 27      declare @stringStr nvarchar(100)
 28 
 29 set @selectNum=cast@selectTopNum as int)
 30 --print @classId
 31 set @stringStr=' classId='+@classId
 32 set @isTopNum=' isTop='+@isTopNum
 33 set @topClassNum=' top '+cast@topNum as varchar(20))
 34 set @selectTopNum=' top '+  cast@selectTopNum as varchar(20))
 35 
 36  --create temp table begin
 37  set @sql=' create table t#  (tid int identity(1,1) ,id int  ,title nvarchar(100),userId int,classId int,isTop int  )'
 38  --print @sql
 39  exec sp_executeSql  @sql
 40   set @sql=' insert into  t#   (id,title,userid,classid,istop) select ' +@topClassNum+' [id],title,userId,classId,isTop from article  where '+ @stringStr+'  and isTop =1 order by addTime desc '
 41  --print @sql
 42  exec sp_executeSql  @sql
 43  
 44  declare @rCount int select @rCount=count(1from t#
 45  --set @rCount=20-@rCount
 46  --print @rCount
 47  if(@rCount<@topNum
 48  begin  delete from t# 
 49  set @sql=' insert into t#(id,title,userid,classid,istop) select  [id],title,userId,classId,isTop from article  where ' + @stringStr+' and istop<'+cast(@topNum as varchar(20))+' order by  istop desc, addTime desc'
 50  --print @sql
 51  exec sp_executeSql  @sql
 52    end
 53  else begin  
 54  set @sql='insert into t#(id,title,userid,classid,istop) select  a.id,a.title,a.userId,a.classId,a.isTop from article a  where a.id not in(select [id] from t#) and '+@stringStr+' and isTop<'+cast@topNum as varchar(20))+'  order by addTime desc'
 55  --print @sql
 56  exec sp_executeSql  @sql
 57    End  
 58  --create temp tble end
 59 
 60 
 61 if(@selectNum<>0)  --@selectNum=0,则表示分页,不然就取selectTopNum条数据
 62         begin
 63  set @sql=' select '+@selectTopNum+' * from t#  order by tid asc'
 64  --print  @sql
 65  exec sp_executeSql @sql
 66         end 
 67  else begin
 68   --print 'begin'
 69   Set @TemSql='Select @RecordCount=Count(1) from t#'
 70         exec sp_executesql @TemSql,N'@RecordCount float output',@RecordCount output
 71 
 72        Set @RowsCount=@RecordCount
 73        Set @TotalPage= ceiling(@RecordCount/@PageSize)
 74   
 75        if(@CurrentPage>@TotalPage)
 76             Set @CurrentPage=@TotalPage
 77        if(@CurrentPage<1
 78             Set @CurrentPage=1
 79        if(@PageSize<1)
 80             Set @PageSize=1
 81        --print(@RecordCount)
 82       if(@CurrentPage=1)
 83           Begin
 84                set Rowcount @PageSize
 85   print @PageSize
 86                set @Sql='select  top '+ cast@PageSize as varchar(20)) +'*   from t# order by tid asc'
 87                --print(@Sql)
 88              exec sp_executeSql @Sql
 89           End
 90       else if(@CurrentPage=@TotalPage)
 91           begin
 92               set @afterRows=@RowsCount-(@CurrentPage-1)*@PageSize
 93               set RowCount @afterRows
 94                   set @Sql='    select top '+cast@afterRows as nvarchar(20))+'  *   from  t#  order by  tid  desc'          
 95              -- print(@Sql)
 96               exec sp_executeSql @Sql
 97           end
 98       else
 99           Begin
100               set @nRd=@PageSize* (@CurrentPage-1)
101              print(@nRd)
102               set RowCount @PageSize             
103               set @Sql='select  *  from t# where tid  not in (select top '+ cast(@nRd as nvarchar(10))+'  tid  from  t# order by tid asc) ' + ' order by  tid  desc' 
104               exec sp_executeSql @Sql
105               --Print(@sql)
106 
107         End 
108 
109 end
110 drop table t#
111 end
112 GO
113 
114 

4、总结:

  很少这么认真的写点东西,呵呵,本人太懒了,其实不是懒,是因为很多时候自己需要学习的东西太多了,这是在项目中遇到的一个问题,其中在写存储过程中也发现一些存储过程需要注意的地方,就是当你定义了一个字符串变量时,做字符串的链接时,不能直接和整型变量相接,而要转换后再链接。

PS:文章出处http://www.cnblogs.com/jxcia_Lai,欢迎转载

原文地址:https://www.cnblogs.com/jxcia_Lai/p/1771543.html