热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

分页SQLServer存储过程_sqlserver

*--用存储过程实现的分页程序显示指定表、视图、查询结果的第X页对于表中主键或标识列的情况,直接从原表取数查询,其它情况使用临时表的方法如果视图或查询结果中有主键,不推荐此方法--

/*–用存储过程实现的分页程序显示指定表、视图、查询结果的第X页对于表中主键或标识列的情况,直接从原表取数查询,其它情况使用临时表的方法如果视图或查询结果中有主键,不推荐此方法–邹建 2003.09–*//*–调用示例exec p_show ‘地区资料’exec p_show ‘地区资料’,5,3,’地区编号,地区名称,助记码’,’地区编号’–*//*因为要顾及通用性,所以对带排序的查询语句有一定要求.如果先排序,再出结果.就是:exec p_show ‘select top 100 percent * from 地区资料 order by 地区名称’,5,3,’地区编号,地区名称,助记码’,’地区名称’–查询语句加上:top 100 percent //top时*/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 [email protected] nvarchar(4000),–[email protected] int=10,–每页的大小(行数)@PageCurrent int=1,–[email protected] nvarchar (4000)=”,–要显示的字段列表,如果查询结果有标识字段,需要指定此值,[email protected] 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 ‘ ‘[email protected] end,@FdOrder=case isnull(@FdOrder,”) when ” then ” else ‘ order by ‘[email protected] end,@QueryStr=case when @Obj_ID is not null then ‘ ‘[email protected] else ‘ (‘[email protected]+’) a’ end–如果显示第一页,可以直接用top来完成if @PageCurrent=1beginselect @Id1=cast(@PageSize as varchar(20))exec(‘select top ‘[email protected][email protected]+’ from ‘[email protected][email protected])returnend–如果是表,则检查表中是否有标识更或主键if @Obj_ID is not null and objectproperty(@Obj_ID,’IsTable’)=1beginselect @Id1=cast(@PageSize as varchar(20)),@Id2=cast((@PageCurrent-1)[email protected] as varchar(20))select @FdName=name from syscolumns where [email protected]_ID and status=0x80if @@rowcount=0–如果表中无标识列,则检查表中是否有主键beginif not exists(select 1 from sysobjects where [email protected]_ID and xtype=’PK’)goto lbusetemp–如果表中无主键,则用临时表处理select @FdName=name from syscolumns where [email protected]_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 [email protected]_ID)))if @@rowcount>1–检查表中的主键是否为复合主键beginselect @strfd=”,@strjoin=”,@strwhere=”select @[email protected]+’,[‘+name+’]’,@[email protected]+’ and a.[‘+name+’]=b.[‘+name+’]’,@[email protected]+’ and b.[‘+name+’] is null’from syscolumns where [email protected]_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 [email protected]_ID)))select @strfd=substring(@strfd,2,2000),@strjoin=substring(@strjoin,5,4000),@strwhere=substring(@strwhere,5,4000)goto lbusepkendendendelsegoto lbusetemp/*–使用标识列或主键为单一字段的处理方法–*/lbuseidentity:exec(‘select top ‘[email protected][email protected]+’ from ‘[email protected]+’ where ‘[email protected]+’ not in(select top ‘[email protected]+’ ‘[email protected]+’ from ‘[email protected][email protected]+’)’[email protected])return/*–表中有复合主键的处理方法–*/lbusepk:exec(‘select ‘[email protected]+’ from(select top ‘[email protected]+’ a.* from(select top 100 percent * from ‘[email protected][email protected]+’) aleft join (select top ‘[email protected]+’ ‘[email protected]+’ from ‘[email protected][email protected]+’) b on ‘[email protected]+’where ‘[email protected]+’) a’)return/*–用临时表处理的方法–*/lbusetemp:select @FdName='[ID_’+cast(newid() as varchar(40))+’]’,@Id1=cast(@PageSize*(@PageCurrent-1) as varchar(20)),@Id2=cast(@[email protected] as varchar(20))exec(‘select ‘[email protected]+’=identity(int,0,1),’[email protected]+’into #tb from’[email protected][email protected]+’select ‘[email protected]+’ from #tb where ‘[email protected]+’ between ‘[email protected]+’ and ‘[email protected])GO

欢迎大家阅读《分页 sqlserver存储过程_sqlserver》,跪求各位点评,若觉得好的话请收藏本文,by



推荐阅读
  • 本文介绍了Oracle存储过程的基本语法和写法示例,同时还介绍了已命名的系统异常的产生原因。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 函数:每个DBMS都有特定的函数。举例说明:DBMS函数的差异函数语法提取串的组中部分数据类型转换Access使用MID(); ... [详细]
  • 点此学习更多SQL相关函数与字符串处理函数mysql函数一、简明总结ASCII(char)        返回字符的ASCII码值BIT_LENGTH(str)      返回字 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • 本文介绍了在mac环境下使用nginx配置nodejs代理服务器的步骤,包括安装nginx、创建目录和文件、配置代理的域名和日志记录等。 ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • 本文介绍了一个React Native新手在尝试将数据发布到服务器时遇到的问题,以及他的React Native代码和服务器端代码。他使用fetch方法将数据发送到服务器,但无法在服务器端读取/获取发布的数据。 ... [详细]
  • MySQL多表数据库操作方法及子查询详解
    本文详细介绍了MySQL数据库的多表操作方法,包括增删改和单表查询,同时还解释了子查询的概念和用法。文章通过示例和步骤说明了如何进行数据的插入、删除和更新操作,以及如何执行单表查询和使用聚合函数进行统计。对于需要对MySQL数据库进行操作的读者来说,本文是一个非常实用的参考资料。 ... [详细]
  • 本文主要介绍了gym102222KVertex Covers(高维前缀和,meet in the middle)相关的知识,包括题意、思路和解题代码。题目给定一张n点m边的图,点带点权,定义点覆盖的权值为点权之积,要求所有点覆盖的权值之和膜qn小于等于36。文章详细介绍了解题思路,通过将图分成两个点数接近的点集L和R,并分别枚举子集S和T,判断S和T能否覆盖所有内部的边。文章还提到了使用位运算加速判断覆盖和推导T'的方法。最后给出了解题的代码。 ... [详细]
  • PreparedStatement防止SQL注入
    添加数据:packagecom.hyc.study03;importcom.hyc.study02.utils.JDBCUtils;importjava.sql ... [详细]
  • php设置数组大小_【大厂必备】2020超经典PHP面试题
    结合我自己这段时间的面试情况,面对的一些php面试题列举出来,基本上结合自己的看法回答的,不妥的地方请大家指出去,与大家一起 ... [详细]
  • mysql自动打开文件_让docker中的mysql启动时自动执行sql文件
    本文提要本文目的不仅仅是创建一个MySQL的镜像,而是在其基础上再实现启动过程中自动导入数据及数据库用户的权限设置,并且在新创建出来的容器里自动启动My ... [详细]
  • ***Createdbyjiachenpanon161118.**合法uri*exportfunctionvalidateURL(textval){consturlregex^( ... [详细]
  • Mysql调优的顺序及面试问题总结
    文章目录一、调优相关1.第一步:本地explain线上查询遇到的第一个坑:遇到的第二个坑:2.第二步:覆盖索引3.第三步&# ... [详细]
author-avatar
白日做梦家_
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有