sqlserver 通用存储过程分页代码(附使用ROW_NUMBER()和不使用ROW_NUMBER()两种情况性能分析
-- =============================================
-- Author: <jiangrod>
-- Create date: <2010-03-03>
-- Description: <SQL2000通用分页存储过程>调用方法: sp_Pager2000 'xtest','*','ID','ORDER BY ID ASC','xname like ''%222name%''',3,20,0,0
-- =============================================
ALTER PROCEDURE [dbo].[sp_Pager2000]
@tblName varchar(255), -- 表名如:'xtest'
@strGetFields varchar(1000) = '*', -- 需要返回的列如:'xname,xdemo'
@pkName nvarchar(50)='ID', -- 主键名
@strOrder varchar(255)='', -- 排序的字段名如:'order by id desc'
@strWhere varchar(1500) = '', -- 查询条件(注意:不要加where)如:'xname like ''%222name%'''
@pageIndex int = 1, -- 页码如:2
@pageSize int = 20, -- 每页记录数如:20
@recordCount int=0 out, -- 记录总数
@doCount bit=0 -- 非0则统计,为0则不统计(统计会影响效率)
AS
Declare @sql nvarchar(4000)
Declare @sqltemp nvarchar(1000)
set @sql=' From '+@tblName
if(@strWhere!='') set @sql=@sql+' Where '+@strWhere
declare @strCount nvarchar(1000)
--总记录条数
if(@doCount!=0)
begin
if(@strWhere !='')
begin
set @strCount='set @num=(select count(1) from '+ @tblName + ' where '+@strWhere+' )'
end
else
begin
set @strCount='set @num=(select count(1) from '+ @tblName + ' )'
end
EXECUTE sp_executesql @strCount ,N'@num INT output',@RecordCount output
end
if @strWhere !=''
begin
set @strWhere=' where '+@strWhere
end
if (@PageIndex>1)
begin
set @sqltemp=@PKName+' not in (Select Top '+cast((@PageSize*(@PageIndex-1)) as nvarchar)+' '+@PKName+' '+@sql
if(@strOrder!='')set @sqltemp=@sqltemp+' '+@strOrder
set @sqltemp=@sqltemp+')'
set @sql='Select Top '+cast(@PageSize as nvarchar)+' '+@strGetFields+' '+@sql
if(@strWhere!='')set @sql=@sql+' And '+@sqltemp
else set @sql=@sql+' Where '+@sqltemp
end
else
begin
set @sql='Select Top '+cast(@PageSize as nvarchar)+' '+@strGetFields+' '+@sql
end
if(@strOrder!='') set @sql=@sql+' '+@strOrder
exec(@sql)
-- =============================================
-- Author: <jiangrod>
-- Create date: <2010-03-03>
-- Description: <SQL2005及后续版本通用分页存储过程>调用方法: sp_Pager2005 'xtest','*','ORDER BY ID ASC','xname like ''%222name%''',2,20,0,0
-- =============================================
ALTER PROCEDURE [dbo].[sp_Pager2005]
@tblName varchar(255), -- 表名如:'xtest'
@strGetFields varchar(1000) = '*', -- 需要返回的列如:'xname,xdemo'
@strOrder varchar(255)='', -- 排序的字段名如:'order by id desc'
@strWhere varchar(1500) = '', -- 查询条件(注意:不要加where)如:'xname like ''%222name%'''
@pageIndex int = 1, -- 页码如:2
@pageSize int = 20, -- 每页记录数如:20
@recordCount int output, -- 记录总数
@doCount bit=0 -- 非0则统计,为0则不统计(统计会影响效率)
AS
declare @strSQL varchar(5000)
declare @strCount nvarchar(1000)
--总记录条数
if(@doCount!=0)
begin
if(@strWhere !='')
begin
set @strCount='set @num=(select count(1) from '+ @tblName + ' where '+@strWhere+' )'
end
else
begin
set @strCount='set @num=(select count(1) from '+ @tblName + ' )'
end
EXECUTE sp_executesql @strCount ,N'@num INT output',@RecordCount output
end
if @strWhere !=''
begin
set @strWhere=' where '+@strWhere
end
set @strSQL='SELECT * FROM (SELECT ROW_NUMBER() OVER ('+@strOrder+') AS ROWID,'
set @strSQL=@strSQL+@strGetFields+' FROM ['+@tblName+'] '+@strWhere
set @strSQL=@strSQL+') AS sp WHERE ROWID BETWEEN '+str((@PageIndex-1)*@PageSize+1)
set @strSQL=@strSQL+' AND '+str(@PageIndex*@PageSize)
exec (@strSQL)