sql 2005分页存储过程实例

/*
-------------寒冰制作---------------
------------------------------------
------------ QQ:81868164 ----------
--- email:hanbing81868164@163.com --
--- 由sql 2000的分页存储过来改造 ---
---- 加入sql 2005里新分页技术 ----
-- 如有更好的实现方法可以自行修改 --
----------- 2006.7.11 --------------
- 版权共享 给我留言号记号就行 ^o^ -
------------------------------------
*/
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 字段名
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@IsCount bit=0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(3000) ='', -- 查询条件 (注意: 不要加 where)
@StartIndex varchar(255),
@maxRows varchar(255)
AS

declare @strSQL varchar(3000) -- 主语句
declare @strCount varchar(3000) -- 返回数据的总条数语句
declare @strOrder varchar(3000) -- 排序类型语句

if @OrderType != 0
begin
set @strOrder = ' order by ' + @fldName +' desc'
end
else
begin
set @strOrder = ' order by ' + @fldName +' asc'
end

set @strSQL ='select * from (select *, ROW_NUMBER() OVER ('+ @strOrder +') as Pos from ' + @tblName + ') as T where T.Pos > ('+ @StartIndex +') and T.Pos <'+ @maxRows
set @strCount ='select count(*) as Total from ' + @tblName + ' as count'

if @strWhere !=''
begin
set @strSQL ='select * from (select *, ROW_NUMBER() OVER ('+ @strOrder +') as Pos from ' + @tblName + ' where ' + @strWhere + ') as T where T.Pos > ('+ @StartIndex +') and T.Pos <'+ @maxRows
set @strCount ='select count(*) as Total from ' + @tblName + ' as count where ' + @strWhere
end

if @IsCount!=0 ----按需要返回数据的总条数
begin
exec (@strCount)
end

exec (@strSQL)

代码还在完善中

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[t]') and objectproperty(id, n'isusertable') = 1)

drop table [dbo].[t]

go

create table [dbo].[t] (

[id] [int] identity (1, 1) not null ,

[lid] [nvarchar] (50) ,

[name] [nvarchar] (50) ,

[type] [int] not null

)

go

insert into t (lid,[name],[type])

select "11","城站火车站",0

union all select "11","葵巷建国路口",0

union all select "11","菜市桥",0

union all select "11","潮鸣寺巷",0

union all select "11","宝善桥建国路口",0

union all select "11","宝善桥",0

union all select "11","市体育馆",0

union all select "11","武林广场",0

union all select "11","武林门",0

union all select "11","武林们马塍路口",0

union all select "11","八字桥",0

union all select "11","浙大西溪校区",0

union all select "11","庆丰村",0

union all select "11","教工路口",0

union all select "11","花园新村",0

union all select "11","浙江工商大学",0

union all select "11","电子科技大学",0

union all select "11","翠苑新村",0

union all select "57","大关小区",0

union all select "57","通信市场",0

union all select "57","德胜新村",0

union all select "57","潮王路口",0

union all select "57","朝晖五区",0

union all select "57","朝晖三区",0

union all select "57","西湖文化广场东",0

union all select "57","武林广场",0

union all select "57","武林小广场",0

union all select "57","半道红",0

union all select "57","文三路口",0

union all select "57","上宁桥",0

union all select "57","花园新村",0

union all select "57","浙江工商大学",0

union all select "57","文一路口",0

union all select "57","教工路北口",0

union all select "57","大关桥西",0

union all select "57","上塘路口",0

union all select "57","大关西六苑",0

union all select "57","香积寺路口",0

union all select "57","大关小区",0

union all select "14","武林小广场",0

union all select "14","昌化新村",0

union all select "14","长寿桥",0

union all select "14","延安路口",0

union all select "14","中大广场",0

union all select "14","众安桥",0

union all select "14","浙一医院",0

union all select "14","大学路北口",0

union all select "14","庆春门",0

union all select "14","金衙庄",0

union all select "14","总管塘",0

union all select "14","华东家具市场",0

union all select "14","近江村",0

union all select "14","汽车南站",0

union all select "14","汽车南站",1

union all select "14","近江村",1

union all select "14","华东家具市场",1

union all select "14","总管塘",1

union all select "14","金衙庄",1

union all select "14","庆春门",1

union all select "14","大学路北口",1

union all select "14","浙一医院",1

union all select "14","众安桥",1

union all select "14","中大广场",1

union all select "14","延安路口",1

union all select "14","长寿桥",1

union all select "14","昌化新村",1

union all select "14","武林小广场",1

union all select "k105","火车东站",0

union all select "k105","汽车东站",0

union all select "k105","严家弄",0

union all select "k105","景芳五区",0

union all select "k105","景御路口",0

union all select "k105","庆春东路",0

union all select "k105","采荷新村",0

union all select "k105","观音塘小区",0

union all select "k105","总管塘",0

union all select "k105","章家桥",0

union all select "k105","浙二医院",0

union all select "k105","官巷口",0

union all select "k105","湖滨",0

union all select "k105","胜利剧院",0

union all select "k105","孩儿巷",0

union all select "k105","延安新村",0

union all select "k105","武林小广场",0

union all select "k105","杭州大厦",0

union all select "k105","中北桥",0

union all select "k105","施家桥",0

union all select "k105","建国北路文晖路口",0

union all select "k105","文晖大桥东",0

union all select "k105","机神村",0

union all select "k105","天城路口",0

union all select "k105","新塘路口",0

union all select "k105","火车东站",0

union all select "39","闸口",0

union all select "39","水澄桥",0

union all select "39","海月桥",0

union all select "39","美政桥",0

union all select "39","复兴路北口",0

union all select "39","三廊庙",0

union all select "39","木材新村",0

union all select "39","二凉亭",0

union all select "39","望江门外",0

union all select "39","汽车南站",0

union all select "39","近江村",0

union all select "39","华东家具市场",0

union all select "39","总管塘",0

union all select "39","城站火车站",0

union all select "k101","城站火车站",0

union all select "k101","总管塘",0

union all select "k101","观音塘小区",0

union all select "k101","采荷新村",0

union all select "k101","红菱新村",0

union all select "k101","凤起东路口",0

union all select "k101","双菱路北口",0

union all select "k101","市红会医院",0

union all select "k101","建国路口",0

union all select "k101","新华路口",0

union all select "k101","中北路口",0

union all select "k101","延安路口",0

union all select "k101","浙大湖滨校区",0

union all select "k101","昌化新村",0

union all select "k101","市府大楼",0

union all select "k101","武林门马塍路口",0

union all select "k101","八字桥",0

union all select "k101","浙大西溪校区",0

union all select "k101","庆丰村",0

union all select "k101","玉古路天目山路口",0

union all select "k101","西湖体育馆",0

union all select "21/k21","城站火车站",0

union all select "21/k21","章家桥",0

union all select "21/k21","新城隧道东口",0

union all select "21/k21","解放路秋涛路口",0

union all select "21/k21","采荷新村",0

union all select "21/k21","红菱新村",0

union all select "21/k21","双菱路北口",0

union all select "21/k21","市红会医院",0

union all select "21/k21","建国路口",0

union all select "21/k21","新华路口",0

union all select "21/k21","中北路口",0

union all select "21/k21","延安路口",0

union all select "21/k21","浙大湖滨校区",0

union all select "21/k21","昌化新村",0

union all select "21/k21","市府大楼",0

union all select "21/k21","武林门马塍路口",0

union all select "21/k21","八字桥",0

union all select "21/k21","浙大西溪校区",0

union all select "21/k21","庆丰村",0

union all select "21/k21","跑马场",0

union all select "21/k21","黄龙体育中心",0

union all select "21/k21","浙大附中",0

union all select "21/k21","求是路",0

union all select "21/k21","西湖体育馆",0

union all select "58/k58","大关小区",0

union all select "58/k58","上塘路香积寺路口",0

union all select "58/k58","大关西六苑",0

union all select "58/k58","上塘路口",0

union all select "58/k58","大关桥西",0

union all select "58/k58","教工路北口",0

union all select "58/k58","文一路口",0

union all select "58/k58","浙江工商大学",0

union all select "58/k58","花园新村",0

union all select "58/k58","上宁桥",0

union all select "58/k58","文三新村",0

union all select "58/k58","八字桥",0

union all select "58/k58","武林门马塍路口",0

union all select "58/k58","武林小广场",0

union all select "58/k58","武林广场",0

union all select "58/k58","中北桥",0

union all select "58/k58","朝晖一区",0

union all select "58/k58","朝晖三区",0

union all select "58/k58","朝晖五区",0

union all select "58/k58","潮王路口",0

union all select "58/k58","德胜新村",0

union all select "58/k58","通信市场",0

union all select "58/k58","大关小区",0

union all select "k101","西湖体育馆",1

union all select "k101","玉古路天目山路口",1

union all select "k101","庆丰村",1

union all select "k101","浙大西溪校区",1

union all select "k101","八字桥",1

union all select "k101","武林门马塍路口",1

union all select "k101","市府大楼",1

union all select "k101","昌化新村",1

union all select "k101","浙大湖滨校区",1

union all select "k101","延安路口",1

union all select "k101","中北路口",1

union all select "k101","新华路口",1

union all select "k101","建国路口",1

union all select "k101","市红会医院",1

union all select "k101","双菱路北口",1

union all select "k101","凤起东路口",1

union all select "k101","红菱新村",1

union all select "k101","采荷新村",1

union all select "k101","观音塘小区",1

union all select "k101","总管塘",1

union all select "k101","城站火车站",1

/****** object: stored procedure dbo.search script date: 2005-9-8 10:28:35 ******/

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[search]') and objectproperty(id, n'isprocedure') = 1)

drop procedure [dbo].[search]

go

set quoted_identifier off

go

set ansi_nulls on

go

/****** object: stored procedure dbo.search script date: 2005-9-8 10:28:35 ******/

create proc search

@name1 nvarchar(50),

@name2 nvarchar(50)

as

--中转站

create table #tmp

(

tmp_id int identity(1,1),

tmp_name nvarchar(50)

)

--站点队列

create table #tmp1

(

tmp1_id int identity(1,1),

tmp1_name nvarchar(50)

)

--查找结果

create table #result

(

r_id int,

r_lid nvarchar(50),

r_name nvarchar(50),

r_type int

)

--直达

insert into #result

select c.* from t a,t b,t c where

a.lid=b.lid and a.[type]=b.[type] and a.idand a.[name] = @name1 and b.[name] = @name2

and c.id>=a.id and c.id<=b.id order by c.id

if @@rowcount>0 begin

select * from #result

end

else begin

--换车

declare @currenname nvarchar(50)

set @currenname = @name1

change:

/*

--车次入栈

insert into #tmp (tmp_lid)

select distinct lid from t where [name] = @currenname

declare @currenbus nvarchar(50)

select top 1 @currenbus = tmp_lid from #tmp

*/

insert into #tmp1 (tmp1_name)

select distinct b.[name] from t a,t b where a.[name] = @currenname and b.lid = a.lid and b.[name] <> @currenname

insert into #tmp (tmp_name)

select d.[tmp1_name] from t a,t b,t c, #tmp1 d where

a.lid=b.lid and a.[type]=b.[type] and a.id and a.[name] = d.[tmp1_name] and b.[name] = @name2

and c.id>=a.id and c.id<=b.id

if @@rowcount>0 begin

select distinct c.* from t a,t b,t c,#tmp d where

a.lid=b.lid and a.[type]=b.[type] and a.id and a.[name] = @name1 and b.[name] = d.tmp_name

and c.id>=a.id and c.id<=b.id order by c.id

select distinct c.* from t a,t b,t c,#tmp d where

a.lid=b.lid and a.[type]=b.[type] and a.id and a.[name] = d.tmp_name and b.[name] = @name2

and c.id>=a.id and c.id<=b.id order by c.id

end

--select * from #tmp

end

drop table #result

drop table #tmp1

drop table #tmp

go

set quoted_identifier off

go

set ansi_nulls on

go

exec search '文一路口','总管塘'

转载于:https://www.cnblogs.com/Nina-piaoye/archive/2006/09/18/507231.html

sql server教程sql存储过程和算法相关推荐

  1. 通过 Visual Studio 对 SQL Server 中的存储过程设置断点并进入存储过程对其进行调试...

    通过 Visual Studio 的 Professional 和 Team System 版本,我们可以对 SQL Server 中的存储过程设置断点并进入存储过程对其进行调试,这样我们可以象调试应 ...

  2. SQL Server基础之存储过程

    阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储过程   简单来说,存储过程就是一条或 ...

  3. Sql Server实用操作-存储过程精解

    CREATE PROCEDURE 创建存储过程,存储过程是保存起来的可以接受和返回用户提供的参数的 Transact-SQL 语句的集合.可以创建一个过程供永久使用,或在一个会话中临时使用(局部临时过 ...

  4. SQL SERVER 通用分页存储过程

    SQL SERVER 通用分页存储过程 从SQLSERVER 2005开始,提供了Row_Number()函数,利用函数生成的Index来处理分页,按照正常的逻辑思维都是传pageIndex和page ...

  5. Sql Server 查看所有存储过程或视图的位置及内容

    前一段时间,有个需求,就是对部分表进行了分库,所以,原库里面的存储过程.视图和函数里的表名等信息也要跟着更新,刚开始尝试手动检查了几个存储过程,可发现存储过程太多,检查起来效率很低,还容易出错,况且还 ...

  6. sql server中的存储过程调试

    调试: 在SQL QUERY中->对象浏览器->存储过程->选择要调试的存储过程->右击->选择调试(debug)->输入参数->--设置完你的参数值后--点 ...

  7. Sql Server系列:存储过程

    1 存储过程简介 存储过程是使用T-SQL代码编写的代码段.在存储过程中,可以声明变量.执行条件判断语句等其他编程功能.在MS SQL Server 2012中存储过程主要分三类:系统存储过程.自定义 ...

  8. 在Visual Studio 2005中调试SQL Server 2005的存储过程

    介绍 SQL Server 2000可以让我们在查询分析器中直接调试存储过程( 更多的信息请参看:用SQL Server 2000的查询分析器调试存储过程) 到了现在的SQL Server 2005, ...

  9. sql server内置存储过程、查看系统信息

    1.检索关键字:sql server内置存储过程,sql server查看系统信息 2.查看磁盘空间:EXEC master.dbo.xp_fixeddrives , --查看各个数据库所在磁盘情况S ...

最新文章

  1. 玩客云如何设置文件存储到副盘_如何存储自己的资料:低成本的小型存储方案...
  2. JSP中javaBean和Servlet理解
  3. coreelec ssh访问被拒绝_GitLab官网在线仓库SSH连接故障排查和经验总结实例
  4. Android攻城狮SurfaceView
  5. 揭秘视频千倍压缩背后的技术原理之预测技术
  6. 服务器设备性能说明,OMC服务器硬件性能和配置说明.doc
  7. HttpHandler:给指定路径下的图片添加水印显示
  8. Ubuntu Server 18.04 与 MySQL 5.7
  9. 新iPhone有望再迎大变化:终于要用Type-C接口了?
  10. linux下查看mysql数据库的字段类型_系统运维|[小白技巧]如何在Linux上检查MySQL数据表的存储引擎类型...
  11. 2020年我对大前端的认知,前端开发有哪些变化
  12. 南京铁道学院计算机应用,南京铁道职业技术学院铁道交通运营管理专业
  13. typedef struct Lnode{ Elemtype data; struct Lnode *next; } Lnode,*Linklist;
  14. wechaty-微信聊天机器人-调研分析
  15. linux红帽子怎么配置dhcp,红帽7 配置dhcp服务器
  16. 如何对工厂设备进行精准化管理?
  17. Tablespace free space
  18. acrobat如何设置可以使pdf输入页码时自动跳到正文对应的页码
  19. SIO_KEEPALIVE_VALS 用户异常掉线检测
  20. R 安装 “umap-learn“ python 包

热门文章

  1. Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
  2. Spring和SpringMVC的总结
  3. Spring boot 入门篇
  4. centos下yum安装nginx
  5. Nginx:安装+测试
  6. 3.程序的局部性原理
  7. 【Scratch】青少年蓝桥杯_每日一题_3.23_骰子
  8. Java 洛谷 P1008 三连击
  9. Java数字反转(编程题)
  10. c语言cin n1 n2,牛客等级之题N1 追债之旅 - N2 Rinne Loves Study(8.6场)