您现在的位置是:首页 >

分页查询sql 分页SQLServer存储过程

火烧 2021-05-12 21:42:27 1042
分页SQLServer存储过程       /*  因为要顾及通用性 所以对带排序的查询语句有一定要求 如果先排序 再出结果 就是:  exec _ how elect to erce t * fro

分页SQLServer存储过程  

  

  

  /*

  因为要顾及通用性 所以对带排序的查询语句有一定要求 如果先排序 再出结果 就是:

  exec p_show select top percent * from 地区资料 order by 地区名称 地区编号 地区名称 助记码 地区名称

   查询语句加上:top percent //top时

  */

  if exists (select * from dbo sysobjects where id = object_id(N [dbo] [p_show] ) and OBJECTPROPERTY(id N IsProcedure ) = )

  drop procedure [dbo] [p_show]

  GO

  CREATE Proc p_show

  @QueryStr nvarchar( ) 表名 视图名 查询语句

  @PageSize int= 每页的大小(行数)

  @PageCurrent int= 要显示的页

  @FdShow nvarchar ( )= 要显示的字段列表 如果查询结果有标识字段 需要指定此值 且不包含标识字段

分页查询sql 分页SQLServer存储过程

  @FdOrder nvarchar ( )= 排序字段列表

  as

  declare @FdName nvarchar( ) 表中的主键或表 临时表中的标识列名

   @Id varchar( ) @Id varchar( ) 开始和结束的记录号

   @Obj_ID int 对象ID

   表中有复合主键的处理

  declare @strfd nvarchar( ) 复合主键列表

   @strjoin nvarchar( ) 连接字段

   @strwhere nvarchar( ) 查询条件

  select @Obj_ID=object_id(@QueryStr)

   @FdShow=case isnull(@FdShow ) when then * else

  [url=mailto: +@FdShow] +@FdShow[/url]

  end

   @FdOrder=case isnull(@FdOrder ) when then else order by

  [url=mailto: +@FdOrder] +@FdOrder[/url]

  end

   @QueryStr=case when @Obj_ID is not null then

  [url=mailto: +@QueryStr] +@QueryStr[/url]

  else (

  [url=mailto: +@QueryStr+ ] +@QueryStr+ [/url]

  ) a end

   如果显示第一页 可以直接用top来完成

  if @PageCurrent=

  begin

  select @Id =cast(@PageSize as varchar( ))

  exec( select top

  [url=mailto: +@Id +@FdShow+ ] +@Id +@FdShow+ [/url]

  from

  [url=mailto: +@QueryStr+@FdOrder] +@QueryStr+@FdOrder[/url]

  )

  return

  end

   如果是表 则检查表中是否有标识更或主键

  if @Obj_ID is not null and objectproperty(@Obj_ID IsTable )=

  begin

  select @Id =cast(@PageSize as varchar( ))

   @Id =cast((@PageCurrent )*@PageSize as varchar( ))

  select @FdName=name from syscolumns where

  [url=mailto:id=@Obj_ID]id=@Obj_ID[/url]

  and status= x

  if @@rowcount= 如果表中无标识列 则检查表中是否有主键

  begin

  if not exists(select from sysobjects where

  [url=mailto:parent_obj=@Obj_ID]parent_obj=@Obj_ID[/url]

  and xtype= PK )

  goto lbusetemp 如果表中无主键 则用临时表处理

  select @FdName=name from syscolumns where

  [url=mailto:id=@Obj_ID]id=@Obj_ID[/url]

  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

  [url=mailto:parent_obj=@Obj_ID]parent_obj=@Obj_ID[/url]

  )))

  if @@rowcount> 检查表中的主键是否为复合主键

  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

  [url=mailto:id=@Obj_ID]id=@Obj_ID[/url]

  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

  [url=mailto:parent_obj=@Obj_ID]parent_obj=@Obj_ID[/url]

  )))

  select @strfd=substring(@strfd )

   @strjoin=substring(@strjoin )

   @strwhere=substring(@strwhere )

  goto lbusepk

  end

  end

  end

  else

  goto lbusetemp

  

  lbuseidentity:

  exec( select top

  [url=mailto: +@Id +@FdShow+ ] +@Id +@FdShow+ [/url]

  from

  [url=mailto: +@QueryStr] +@QueryStr[/url]

  + where

  [url=mailto: +@FdName+ ] +@FdName+ [/url]

  not in(select top

  [url=mailto:+@Id + ]+@Id + [/url]

  [url=mailto: +@FdName+ ] +@FdName+ [/url]

  from

  [url=mailto: +@QueryStr+@FdOrder] +@QueryStr+@FdOrder[/url]

  + ) +@FdOrder

  )

  return

  

  lbusepk:

  exec( select

  [url=mailto: +@FdShow+ ] +@FdShow+ [/url]

  from(select top

  [url=mailto: +@Id + ] +@Id + [/url]

  a * from

  (select top percent * from

  [url=mailto: +@QueryStr+@FdOrder+ ] +@QueryStr+@FdOrder+ [/url]

  ) a

  left join (select top

  [url=mailto: +@Id + ] +@Id + [/url]

  [url=mailto: +@strfd+ ] +@strfd+ [/url]

  from

  [url=mailto: +@QueryStr+@FdOrder+ ] +@QueryStr+@FdOrder+ [/url]

  ) b on

  [url=mailto: +@strjoin+ ] +@strjoin+ [/url]

  where

  [url=mailto: +@strwhere+ ] +@strwhere+ [/url]

  ) a

  )

  return

  

  lbusetemp:

  select @FdName= [ID_ +cast(newid() as varchar( ))+ ]

   @Id =cast(@PageSize*(@PageCurrent ) as varchar( ))

   @Id =cast(@PageSize*@PageCurrent as varchar( ))

  exec( select

  [url=mailto: +@FdName+ =identity(int ) +@FdShow+ ] +@FdName+ =identity(int ) +@FdShow+ [/url]

  into #tb

  [url=mailto:from +@QueryStr+@FdOrder+ ]from +@QueryStr+@FdOrder+ [/url]

  select

  [url=mailto: +@FdShow+ ] +@FdShow+ [/url]

  from #tb where

  [url=mailto: +@FdName+ ] +@FdName+ [/url]

  beeen

  [url=mailto:+@Id + ]+@Id + [/url]

  and

  [url=mailto: +@Id ] +@Id [/url]

  )

lishixinzhi/Article/program/SQLServer/201311/22063  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码