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
(
1
)
from
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