代码改变世界

SQL存储过程分页排序

2010-08-14 19:36  音乐让我说  阅读(571)  评论(0编辑  收藏  举报

代码如下:

 

create procedure [dbo].[CommonDataPager]
(
	--用途:分页查询记录信息
	--以下是几个参数的说明:
	--1.@tbName:表示表名
	--2.@PrimaryKey:表的主键
	--3.@searchCount:返回的结果数
	--4.@fldNames:字段名,多个字段以逗号分割
	--5.@PageSize:每页显示的记录数,如果为0,则表示不要分页,直接得到搜索、排序后的所有记录
	--6.@PageIndex:当前页的索引
	--7.@Filter:搜索条件,即在“where”后面写条件
	--8.@Sort:排序项,即在“order by”后面写排序项
	--9.@joinTable:连接其他表
	@tbName varchar(3000),
	@PrimaryKey varchar (255),
	@searchCount int output,
	@fldNames varchar(3000)='*',
	@PageSize int= 0,
	@PageIndex int= 1,
	@Filter nvarChar (3000)='',
	@Sort varchar (3000)='',
	@joinTable nvarchar(3000)=''			--调用的时候,可以类似如:as a inner join departInfo as b on a.departInfoId=b.Id
)
as
declare @start   nvarchar (100)
declare @end   nvarchar (100)
declare @m_WhereString varchar(4000)
declare @sqlString nvarchar(4000)			--动态拼接的SQL语句
declare @orderString   nvarchar (4000)

if(@PageSize < 0)
begin
	set @PageSize=0;
end
if(@PageIndex < 1)
begin
	set @PageIndex=1;
end
set @start = ltrim(rtrim(Cast((@PageSize*(@PageIndex - 1)+1) as varchar (100))))		--开始记录
set @end =ltrim(rtrim(Cast((@PageSize * @PageIndex) as varchar (100))))				--结束记录
set @m_WhereString=''
if(len(ltrim(rtrim(@Filter))) > 0)
begin
	set @m_WhereString   = ' where ' + @Filter;
end

if (len (ltrim (rtrim (@Sort))) > 0)
begin
	set @orderString   = ' order by ' + @Sort;
end
else
begin
	set @orderString   = ' order by ' + @PrimaryKey + ' asc '
end
declare @sqlStringOutput nvarchar(3000)
set @sqlStringOutput=N'select @searchCount=count(*) from ' + @tbName + ' ' + @joinTable + ' ' + @m_WhereString;
exec sp_executesql @sqlStringOutput,N'@searchCount int output',@searchCount output
if (@PageSize = 0)
begin
	--得到所有的搜索记录,并且不分页
	set @sqlString='select ' + @fldNames + ' from  ' + @tbName + @m_WhereString + ' ' + @orderString;
end
else
begin
	--实现分页效果,并且根据搜索条件过滤、分页、排序
	set @sqlString='select * from (select row_number() over('+ @orderString +') as myNewId,' + @fldNames + ' from ' + @tbName + ' ' + @joinTable + ' ' + @m_WhereString + ' ) as MyTempTable where myNewId between '+ @start +' and '+ @end +' order by myNewId asc' ;                                                              
end
exec(@sqlString)
go



 

新:

 

create procedure [dbo].[CommonDataPager]
(
	--用途:分页查询记录信息
	--以下是几个参数的说明:
	--1.@tbName:表示表名
	--2.@PrimaryKey:表的主键
	--3.@searchCount:返回的结果数
	--4.@fldNames:字段名,多个字段以逗号分割
	--5.@PageSize:每页显示的记录数,如果为0,则表示不要分页,直接得到搜索、排序后的所有记录
	--6.@PageIndex:当前页的索引
	--7.@Filter:搜索条件,即在“where”后面写条件
	--8.@Sort:排序项,即在“order by”后面写排序项
	--9.@joinTable:连接其他表
	@tbName varchar(3000),
	@PrimaryKey varchar (255),
	@searchCount int output,
	@fldNames varchar(3000)='*',
	@PageSize int= 0,
	@PageIndex int= 1,
	@Filter nvarChar (3000)='',
	@Sort varchar (3000)='',
	@joinTable nvarchar(3000)=''			--调用的时候,可以类似如:as a inner join departInfo as b on a.departInfoId=b.Id
)
as
declare @start   nvarchar (100)
declare @end   nvarchar (100)
declare @m_WhereString varchar(4000)
declare @sqlString nvarchar(4000)			--动态拼接的SQL语句
declare @orderString   nvarchar (4000)

if(@PageSize < 0)
begin
	set @PageSize=0;
end
if(@PageIndex < 1)
begin
	set @PageIndex=1;
end
set @start = ltrim(rtrim(Cast((@PageSize*(@PageIndex - 1)+1) as varchar (100))))		--开始记录
set @end =ltrim(rtrim(Cast((@PageSize * @PageIndex) as varchar (100))))				--结束记录
set @m_WhereString=''
if(len(ltrim(rtrim(@Filter))) > 0)
begin
	set @m_WhereString   = ' where ' + @Filter;
end

if (len (ltrim (rtrim (@Sort))) > 0)
begin
	set @orderString   = ' order by ' + @Sort;
end
else
begin
	set @orderString   = ' order by ' + @PrimaryKey + ' asc '
end
declare @sqlStringOutput nvarchar(3000)
set @sqlStringOutput=N'select @searchCount=count(*) from ' + @tbName + ' ' + @joinTable + ' ' + @m_WhereString;
exec sp_executesql @sqlStringOutput,N'@searchCount int output',@searchCount output
if (@PageSize = 0)
begin
	--得到所有的搜索记录,并且不分页
	set @sqlString='select ' + @fldNames + ' from  ' + @tbName + @m_WhereString + ' ' + @orderString;
end
else
begin
	--实现分页效果,并且根据搜索条件过滤、分页、排序
	set @sqlString='select * from (select row_number() over('+ @orderString +') as myNewId,' + @fldNames + ' from ' + @tbName + ' ' + @joinTable + ' ' + @m_WhereString + ' ) as MyTempTable where myNewId between '+ @start +' and '+ @end +' order by myNewId asc' ;                                                              
end
exec(@sqlString)
go


/*
--测试调用存储过程
declare @count int
set @count=0
exec [CommonDataPager] 'UserInfo','Id',@count output,'userName,userAge',15,1,'userName like ''[^n]%'' ','userName desc',''
print @count
*/

 

等待更新...