SQL----动态分页存储过程最终版本
2009-03-22 23:48
459 查看
这些天一直在改进论坛的分页存储过程,运用动态SQL以增强查询字段的灵活性:
1........................主题列表的分页存储过程
ALTER PROCEDURE dbo.SP_GetTopicListByForumId
(@StartIndex int,--每页起始行的顺序号
@EndIndex int,--每页末行的顺序号
@Forum_Id int,--版块ID
@State int,--主题状态
@Issue_Date datetime='1900-1-1',--发布时间
@State_Mode varchar(50)='!=',--主题状态等于方式
@Sort_Type varchar(50)='Topic.Last_Reply_Time',--每个主题的最后回复时间
@Sort_Mode varchar(50)='DESC'--升降序
)
as
set nocount on
create table #indextable
(
id int identity(1,1),
nid int
)
declare @cmd0 nvarchar(2000)
declare @cmd nvarchar(2000)
set rowcount @endIndex
set @cmd0 = 'insert into #indextable(nid)
select Id from Topic WHERE
(Topic.Forum_Id = '+rtrim(@Forum_Id)+') AND
(Topic.State '+rtrim(@State)+')
AND (Issue_Date >''' +convert(varchar(10), @Issue_Date, 120)+ ''')
order by
exec(@cmd0)
set @cmd = 'SELECT Topic.*, UserInfo.User_Name FROM Topic
INNER JOIN UserInfo ON Topic.User_Id = UserInfo.Id
INNER JOIN #indextable t ON Topic.Id=t.nid WHERE
(Topic.Forum_Id = '+rtrim(@Forum_Id)+') AND
(Topic.State '+rtrim(@State)+')
AND (Issue_Date >''' +convert(varchar(10), @Issue_Date, 120)+ ''')
AND (t.id between '+rtrim(@StartIndex)+' and '+rtrim(@EndIndex)+')
order by
print(@cmd0)
exec(@cmd)
set nocount off
RETURN
2........................回帖列表的分页存储过程
ALTER PROCEDURE dbo.SP_GetReplyList
(
@StartIndex int,--每页起始行的顺序号
@EndIndex int,--每页末行的顺序号
@Topic_Id int,--主题ID
@User_Id int--回帖人ID
)
AS
set nocount on
create table #indextable
(
id int identity(1,1),
nid int
)
declare @where varchar(500)
declare @cmd0 nvarchar(2000)
declare @cmd varchar(2000)
set @where = ' ORDER BY Reply.Floor'
set @cmd0 = 'insert into #indextable(nid)
select Id from Reply WHERE (Reply.Topic_Id = '+rtrim(@Topic_Id)+')'
IF(@User_Id != 0)
set @cmd0 = @cmd0 + ' AND (Reply.User_Id = ' +rtrim(@User_Id)+ ')'
set @cmd0 = @cmd0 + @where
exec(@cmd0)
set @cmd = 'SELECT UserInfo.Id as UserId, Reply.Id as ReplyId, Reply.*, UserInfo.*, Ranks.*, Handle.*
FROM Reply INNER JOIN
UserInfo ON Reply.User_Id = UserInfo.Id INNER JOIN
Ranks ON UserInfo.Rank = Ranks.Id INNER JOIN
Handle ON UserInfo.Handle=Handle.Id INNER JOIN
#indextable t ON Reply.Id=t.nid
WHERE (Reply.Topic_Id = '+rtrim(@Topic_Id)+')'
IF(@User_Id != 0)
set @cmd = @cmd + ' AND (Reply.User_Id = ' +rtrim(@User_Id)+ ')'
set @cmd = @cmd + ' AND (t.id between '+rtrim(@StartIndex)+' and '+rtrim(@EndIndex)+')'
set @cmd = @cmd + @where
print(@cmd)
EXEC(@cmd)
set nocount off
RETURN
1........................主题列表的分页存储过程
ALTER PROCEDURE dbo.SP_GetTopicListByForumId
(@StartIndex int,--每页起始行的顺序号
@EndIndex int,--每页末行的顺序号
@Forum_Id int,--版块ID
@State int,--主题状态
@Issue_Date datetime='1900-1-1',--发布时间
@State_Mode varchar(50)='!=',--主题状态等于方式
@Sort_Type varchar(50)='Topic.Last_Reply_Time',--每个主题的最后回复时间
@Sort_Mode varchar(50)='DESC'--升降序
)
as
set nocount on
create table #indextable
(
id int identity(1,1),
nid int
)
declare @cmd0 nvarchar(2000)
declare @cmd nvarchar(2000)
set rowcount @endIndex
set @cmd0 = 'insert into #indextable(nid)
select Id from Topic WHERE
(Topic.Forum_Id = '+rtrim(@Forum_Id)+') AND
(Topic.State '+rtrim(@State)+')
AND (Issue_Date >''' +convert(varchar(10), @Issue_Date, 120)+ ''')
order by
exec(@cmd0)
set @cmd = 'SELECT Topic.*, UserInfo.User_Name FROM Topic
INNER JOIN UserInfo ON Topic.User_Id = UserInfo.Id
INNER JOIN #indextable t ON Topic.Id=t.nid WHERE
(Topic.Forum_Id = '+rtrim(@Forum_Id)+') AND
(Topic.State '+rtrim(@State)+')
AND (Issue_Date >''' +convert(varchar(10), @Issue_Date, 120)+ ''')
AND (t.id between '+rtrim(@StartIndex)+' and '+rtrim(@EndIndex)+')
order by
print(@cmd0)
exec(@cmd)
set nocount off
RETURN
2........................回帖列表的分页存储过程
ALTER PROCEDURE dbo.SP_GetReplyList
(
@StartIndex int,--每页起始行的顺序号
@EndIndex int,--每页末行的顺序号
@Topic_Id int,--主题ID
@User_Id int--回帖人ID
)
AS
set nocount on
create table #indextable
(
id int identity(1,1),
nid int
)
declare @where varchar(500)
declare @cmd0 nvarchar(2000)
declare @cmd varchar(2000)
set @where = ' ORDER BY Reply.Floor'
set @cmd0 = 'insert into #indextable(nid)
select Id from Reply WHERE (Reply.Topic_Id = '+rtrim(@Topic_Id)+')'
IF(@User_Id != 0)
set @cmd0 = @cmd0 + ' AND (Reply.User_Id = ' +rtrim(@User_Id)+ ')'
set @cmd0 = @cmd0 + @where
exec(@cmd0)
set @cmd = 'SELECT UserInfo.Id as UserId, Reply.Id as ReplyId, Reply.*, UserInfo.*, Ranks.*, Handle.*
FROM Reply INNER JOIN
UserInfo ON Reply.User_Id = UserInfo.Id INNER JOIN
Ranks ON UserInfo.Rank = Ranks.Id INNER JOIN
Handle ON UserInfo.Handle=Handle.Id INNER JOIN
#indextable t ON Reply.Id=t.nid
WHERE (Reply.Topic_Id = '+rtrim(@Topic_Id)+')'
IF(@User_Id != 0)
set @cmd = @cmd + ' AND (Reply.User_Id = ' +rtrim(@User_Id)+ ')'
set @cmd = @cmd + ' AND (t.id between '+rtrim(@StartIndex)+' and '+rtrim(@EndIndex)+')'
set @cmd = @cmd + @where
print(@cmd)
EXEC(@cmd)
set nocount off
RETURN
相关文章推荐
- 分页存储过程sql2005以上版本
- 存储过程分页,以及动态sql(Sql server)
- SQL分页参数传值 在存储过程中使用 动态SQL实现
- 简单搞一下 Oracle 存储过程动态SQL之获取查询分页!
- 简单的SQL存储过程分页
- sql存储过程分页
- 关于SQL存储过程分页的研究
- sql存储过程分页
- 存储过程中动态调用SQL语句
- sql 2005分页存储过程
- 存储过程中执行动态Sql语句
- T-SQL存储过程:ROW_NUMBER()分页
- sql server中千万数量级分页存储过程代码
- SQL存储过程分页
- sql存储过程分页有何算法
- [转][SQL]如何实现存储过程中动态加入条件---没想到语句可以这么巧妙
- ms-sql 存储过程分页
- 成熟sql 分页存储过程(UP_General_GetPage)
- SQL 2005 ,sql 2008. 分页存储过程
- [MSSQL]SQL通用存储过程分页,支持多表联合