首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

轨范通用分页存储过程,通用性极强

2012-09-10 
规范通用分页存储过程,通用性极强!昨天写存储过程时遇到一个麻烦,再搜寻答案时搜到了“通用存储过程”,但只

规范通用分页存储过程,通用性极强!
昨天写存储过程时遇到一个麻烦,再搜寻答案时搜到了“通用存储过程”,但只看到部分残缺代码,多方参考后,鄙人对其进行总结规范,得出以下规范存储过程,竟然把本人项目里50多个查询存储过程代替!不敢独享,遂贴出与大家分享,希望对大家有所帮助!!

该存储过程几乎可以涵盖所有查询存储过程(目前本人还没有发现不能使用的)

参数:分页大小,第几页,需要得到的字段 ,需要查询的表 , 查询条件,排序的字段名,排序的类型,主键名称

输入参数即可查询,无论是查询一条记录还是查询多条记录,无论是分页还是不分页,无论是需要查询条件还是不需要查询条件……,都可使用此通用的存储过程!!!
通用性相当好!!



/*通用分页存储过程*/
USE HotelManagementSystem
GO
IF EXISTS(SELECT * FROM sys.objects WHERE NAME='cndoup_GetPageOfRecords')
DROP PROCEDURE cndoup_GetPageOfRecords
GO
--创建存储过程
CREATE PROCEDURE cndoup_GetPageOfRecords
@pageSize int = 20, --分页大小
@currentPage int , --第几页
@columns varchar(1000) = '*', --需要得到的字段 
@tableName varchar(100), --需要查询的表  
@condition varchar(1000) = '', --查询条件, 不用加where关键字
@ascColumn varchar(100) = '', --排序的字段名 (即 order by column asc/desc)
@bitOrderType bit = 0, --排序的类型 (0为升序,1为降序)
@pkColumn varchar(50) = '' --主键名称

AS
BEGIN --存储过程开始
DECLARE @strTemp varchar(300)
DECLARE @strSql varchar(5000) --该存储过程最后执行的语句
DECLARE @strOrderType varchar(1000) --排序类型语句 (order by column asc或者order by column desc)

BEGIN
IF @bitOrderType = 1 --降序
BEGIN
SET @strOrderType = ' ORDER BY '+@ascColumn+' DESC'
SET @strTemp = '<(SELECT min'
END
ELSE--升序
BEGIN
SET @strOrderType = ' ORDER BY '+@ascColumn+' ASC'
SET @strTemp = '>(SELECT max'
END

IF @currentPage = 1--第一页
BEGIN
IF @condition != ''
SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@columns+' FROM '+@tableName+
' WHERE '+@condition+@strOrderType
ELSE
SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@columns+' FROM '+@tableName+@strOrderType
END

ELSE-- 其他页
BEGIN
IF @condition !=''
SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@columns+' FROM '+@tableName+
' WHERE '+@condition+' AND '+@pkColumn+@strTemp+'('+@pkColumn+')'+' FROM (SELECT TOP '+STR((@currentPage-1)*@pageSize)+
' '+@pkColumn+' FROM '+@tableName+@strOrderType+') AS TabTemp)'+@strOrderType
ELSE
SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@columns+' FROM '+@tableName+
' WHERE '+@pkColumn+@strTemp+'('+@pkColumn+')'+' FROM (SELECT TOP '+STR((@currentPage-1)*@pageSize)+' '+@pkColumn+
' FROM '+@tableName+@strOrderType+') AS TabTemp)'+@strOrderType
END

END
EXEC (@strSql)
END

--存储过程结束

/*测试*/
--分页得到客房信息列表测试
EXEC cndoup_GetPageOfRecords 20,2,'房间号=RoomNum,
房间状态=(SELECT RoomTypeDes FROM RoomType WHERE RoomTypeID=Room.RoomTypeID),
房间状态=(SELECT RSDec FROM RoomStatus WHERE RoomStatusID=Room.RoomStatusID),
床位数=BedNum,
楼层=Floors,
描述=RoomDes,
备注=RoomRemark','Room','','RoomID',0,'RoomID'


--根据房间号得到客房信息测试
EXEC cndoup_GetPageOfRecords 1,1,'房间号=RoomNum,
房间状态=(SELECT RoomTypeDes FROM RoomType WHERE RoomTypeID=Room.RoomTypeID),
房间状态=(SELECT RSDec FROM RoomStatus WHERE RoomStatusID=Room.RoomStatusID),
BedNum,
Floors,
RoomDes,
RoomRemark','Room','RoomNum=304','RoomID',0,'RoomID'


--·得到客房类型信息列表测试
EXEC cndoup_GetPageOfRecords 10,1,'RoomTypeDes,Price,Area,AddBed,MaxBedNum,BedPrice,HourRoom,HourPrice,Remark','RoomType','','RoomTypeID',0,'RoomTypeID'



[解决办法]
回头看了下,发现一处错误:
/*通用分页存储过程*/ 
USE HotelManagementSystem 
GO 
IF EXISTS(SELECT * FROM sys.objects WHERE NAME='cndoup_GetPageOfRecords') 


DROP PROCEDURE cndoup_GetPageOfRecords 
GO 
--创建存储过程 
CREATE PROCEDURE cndoup_GetPageOfRecords 
@pageSize int = 20, --分页大小 
@currentPage int , --第几页 
@columns varchar(1000) = '*', --需要得到的字段 
@tableName varchar(100), --需要查询的表
@condition varchar(1000) = '', --查询条件, 不用加where关键字 
@ascColumn varchar(100) = '', --排序的字段名 (即 order by column asc/desc) 
@bitOrderType bit = 0, --排序的类型 (0为升序,1为降序) 
@pkColumn varchar(50) = '' --主键名称 

AS 
BEGIN --存储过程开始 
DECLARE @strTemp varchar(300) 
DECLARE @strSql varchar(5000) --该存储过程最后执行的语句 
DECLARE @strOrderType varchar(1000) --排序类型语句 (order by column asc或者order by column desc) 

BEGIN 
IF @bitOrderType = 1 --降序 
BEGIN 
SET @strOrderType = ' ORDER BY '+@ascColumn+' DESC' 
SET @strTemp = ' <(SELECT min' 
END 
ELSE --升序 
BEGIN 
SET @strOrderType = ' ORDER BY '+@ascColumn+' ASC' 
SET @strTemp = '>(SELECT max' 
END 

IF @currentPage = 1 --第一页 
BEGIN 
IF @condition != '' 
SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@columns+' FROM '+@tableName+ 
' WHERE '+@condition+@strOrderType 
ELSE 
SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@columns+' FROM '+@tableName+@strOrderType 
END 

ELSE -- 其他页 
BEGIN 
IF @condition !='' 
SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@columns+' FROM '+@tableName+ 
' WHERE '+@condition+' AND '+@pkColumn+@strTemp+'('+@pkColumn+')'+' FROM (SELECT TOP '+STR((@currentPage-1)*@pageSize)+ 
' '+@pkColumn+' FROM '+@tableName+' WHERE '+@condition+@strOrderType+') AS TabTemp)'+@strOrderType 
ELSE 
SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@columns+' FROM '+@tableName+ 
' WHERE '+@pkColumn+@strTemp+'('+@pkColumn+')'+' FROM (SELECT TOP '+STR((@currentPage-1)*@pageSize)+' '+@pkColumn+ 
' FROM '+@tableName+@strOrderType+') AS TabTemp)'+@strOrderType 
END 

END 
EXEC (@strSql) 
END 

既然是带条件的,在里面内查询时也应该带上这个条件
[解决办法]
针对性的才能提高性能

------2000可用邹老大的方法,05以上版本用row_number进行分页

SQL code
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_show]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[p_show]GOCREATE Proc p_show@QueryStr nvarchar(4000), --表名、视图名、查询语句@PageSize int=10,   --每页的大小(行数)@PageCurrent int=1,   --要显示的页@FdShow nvarchar (4000)='', --要显示的字段列表,如果查询结果有标识字段,需要指定此值,且不包含标识字段@FdOrder nvarchar (1000)='' --排序字段列表asdeclare @FdName nvarchar(250) --表中的主键或表、临时表中的标识列名 ,@Id1 varchar(20),@Id2 varchar(20) --开始和结束的记录号 ,@Obj_ID int    --对象ID--表中有复合主键的处理declare @strfd nvarchar(2000) --复合主键列表 ,@strjoin nvarchar(4000) --连接字段 ,@strwhere nvarchar(2000) --查询条件select @Obj_ID=object_id(@QueryStr) ,@FdShow=case isnull(@FdShow,'') when '' then ' *' else ' '+@FdShow end ,@FdOrder=case isnull(@FdOrder,'') when '' then '' else ' order by '+@FdOrder end ,@QueryStr=case when @Obj_ID is not null then ' '+@QueryStr else ' ('+@QueryStr+') a' end--如果显示第一页,可以直接用top来完成if @PageCurrent=1 begin select @Id1=cast(@PageSize as varchar(20)) exec('select top '+@Id1+@FdShow+' from '+@QueryStr+@FdOrder) returnend--如果是表,则检查表中是否有标识更或主键if @Obj_ID is not null and objectproperty(@Obj_ID,'IsTable')=1begin select @Id1=cast(@PageSize as varchar(20))  ,@Id2=cast((@PageCurrent-1)*@PageSize as varchar(20)) select @FdName=name from syscolumns where id=@Obj_ID and status=0x80 if @@rowcount=0   --如果表中无标识列,则检查表中是否有主键 begin  if not exists(select 1 from sysobjects where parent_obj=@Obj_ID and xtype='PK')   goto lbusetemp  --如果表中无主键,则用临时表处理  select @FdName=name from syscolumns where id=@Obj_ID and colid in(   select colid from sysindexkeys where @Obj_ID=id and indid in(    select indid from sysindexes where @Obj_ID=id and name in(     select name from sysobjects where xtype='PK' and parent_obj=@Obj_ID   )))  if @@rowcount>1  --检查表中的主键是否为复合主键  begin   select @strfd='',@strjoin='',@strwhere=''   select @strfd=@strfd+',['+name+']'    ,@strjoin=@strjoin+' and a.['+name+']=b.['+name+']'    ,@strwhere=@strwhere+' and b.['+name+'] is null'    from syscolumns where id=@Obj_ID and colid in(    select colid from sysindexkeys where @Obj_ID=id and indid in(     select indid from sysindexes where @Obj_ID=id and name in(      select name from sysobjects where xtype='PK' and parent_obj=@Obj_ID    )))   select @strfd=substring(@strfd,2,2000)    ,@strjoin=substring(@strjoin,5,4000)    ,@strwhere=substring(@strwhere,5,4000)   goto lbusepk  end endendelse goto lbusetemp/*--使用标识列或主键为单一字段的处理方法--*/lbuseidentity:  exec('select top '+@Id1+@FdShow+' from '+@QueryStr  +' where '+@FdName+' not in(select top '  +@Id2+' '+@FdName+' from '+@QueryStr+@FdOrder  +')'+@FdOrder  ) return/*--表中有复合主键的处理方法--*/lbusepk:   exec('select '+@FdShow+' from(select top '+@Id1+' a.* from  (select top 100 percent * from '+@QueryStr+@FdOrder+') a  left join (select top '+@Id2+' '+@strfd+'   from '+@QueryStr+@FdOrder+') b on '+@strjoin+'  where '+@strwhere+') a'  ) return/*--用临时表处理的方法--*/lbusetemp:  select @FdName='[ID_'+cast(newid() as varchar(40))+']' ,@Id1=cast(@PageSize*(@PageCurrent-1) as varchar(20)) ,@Id2=cast(@PageSize*@PageCurrent-1 as varchar(20))exec('select '+@FdName+'=identity(int,0,1),'+@FdShow+'  into #tb from'+@QueryStr+@FdOrder+' select '+@FdShow+' from #tb where '+@FdName+' between ' +@Id1+' and '+@Id2 )GO 

热点排行