码迷,mamicode.com
首页 > 其他好文 > 详细

使用高效的存储过程 分页(ination)技术

时间:2015-02-06 16:30:22      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

数据库记录分页列表是大量必须使用的基本技术,因此本文建议你在每个数据库中建立下面的存储过程:

CREATE PROCEDURE xsp_ination
(
@tblName varchar(64),
@strGetFields varchar(256) = ‘*‘,
@fldName varchar(64)=‘‘,
@PageSize int = 20,
@PageIndex int = 1,
@OrderType bit = 1,
@strWhere varchar(256) = ‘‘
)
AS
BEGIN
declare @strSQL varchar(1000)
declare @strTmp varchar(110)
declare @strOrder varchar(400)
SET NOCOUNT ON
if @OrderType != 0
begin
set @strTmp = ‘<(select min‘
set @strOrder = ‘ order by [‘ + @fldName +‘] desc‘
end
else
begin
set @strTmp = ‘>(select max‘
set @strOrder = ‘ order by [‘ + @fldName +‘] asc‘
end
if @PageIndex = 1
begin
if @strWhere != ‘‘
set @strSQL = ‘select top ‘ + str(@PageSize) +‘ ‘+@strGetFields+ ‘ from ‘ + @tblName + ‘ where ‘ + @strWhere + ‘ ‘ + @strOrder
else
set @strSQL = ‘select top ‘ + str(@PageSize) +‘ ‘+@strGetFields+ ‘ from ‘+ @tblName + ‘ ‘+ @strOrder
end
else
begin
set @strSQL = ‘select top ‘ + str(@PageSize) +‘ ‘+@strGetFields+ ‘ from ‘
+ @tblName + ‘ where [‘ + @fldName + ‘]‘ + @strTmp + ‘([‘+ @fldName + ‘]) from (select top ‘ + str((@PageIndex-1)*@PageSize) + ‘ [‘+ @fldName + ‘] from ‘ + @tblName + ‘ ‘ + @strOrder + ‘) as tblTmp)‘+ @strOrder
if @strWhere != ‘‘
set @strSQL = ‘select top ‘ + str(@PageSize) +‘ ‘+@strGetFields+ ‘ from ‘
+ @tblName + ‘ where [‘ + @fldName + ‘]‘ + @strTmp + ‘([‘
+ @fldName + ‘]) from (select top ‘ + str((@PageIndex-1)*@PageSize) + ‘ [‘
+ @fldName + ‘] from ‘ + @tblName + ‘ where ‘ + @strWhere + ‘ ‘
+ @strOrder + ‘) as tblTmp) and ‘ + @strWhere + ‘ ‘ + @strOrder
end
EXEC (@strSQL)
if @@error=0 return 1
SET NOCOUNT OFF
END
GO

 

使用方法是(C#): 
sql = "EXEC [dbo].[xsp_ination] \"tblNEWS\",\"*\",\"id\",40," + pindex.ToString() + ",1,\"iType=" + type.ToString();
SqlDataReader sr = ExecuteReader(sql);
while (sr.Read())
{
   ...
}
sr.Close();

使用高效的存储过程 分页(ination)技术

标签:

原文地址:http://www.cnblogs.com/luyiwei/p/4277393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!