文本版|topic 高级搜索
   名人堂 帮助 论坛制度 意见反馈 | 首页 博客 周新贴 招聘 专题 新闻
RSS 底部
 
社区导航: 专家门诊   网络技术   操作系统   数据库   程序设计   系统应用   考试认证   CIO及信息化   站长交流   综合交流   下载基地  51CTO产品服务 设为首页 | 收藏本站
51CTO技术论坛» 微软SQL Server专区 » SQL Server应用开发 » 教程SQL存储过程和算法       [ 打印]  [ 订阅]  [ 收藏]  [ 推荐给朋友]   [ 本帖文本页]

论坛跳转:
     
标题: [转载] 教程SQL存储过程和算法  ( 查看:583  回复:7 )   
 
单色转换
新新人类  点击可查看详细



帖子 39
精华 0
无忧币 161
积分 119
阅读权限 20
注册日期 2007-9-17
最后登录 2007-12-2 离线

[查看资料]  [发短消息]  [Blog
       
发表于:2007-12-1 13:27   标题:教程SQL存储过程和算法
上一帖 |
代码还在完善中

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
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
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
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.idEND
--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 '文一路口','总管塘'



网络工程师到底该不该去考CCIE认证?
2007-12-1 13:271楼
[ 顶部 ]
 
zxb218
新新人类  点击可查看详细



帖子 109
精华 0
无忧币 99
积分 112
阅读权限 20
注册日期 2007-12-13
最后登录 2008-7-20 离线

[查看资料]  [发短消息]  [Blog
       
发表于:2008-1-1 20:56 
顶顶,怎么好的东西没人顶



网络工程师到底该不该去考CCIE认证?
2008-1-1 20:562楼
[ 顶部 ]
 
zxjsinlan
新新人类  点击可查看详细



帖子 4
精华 0
无忧币 8
积分 4
阅读权限 20
注册日期 2007-8-11
最后登录 2008-1-14 离线

[查看资料]  [发短消息]  [Blog
       
发表于:2008-1-3 16:19 
關鍵是看不懂,怎麼頂你下



网络工程师到底该不该去考CCIE认证?
2008-1-3 16:193楼
[ 顶部 ]
 
ncrr
初级工程师  点击可查看详细



帖子 2777
精华 0
无忧币 2778
积分 3193
阅读权限 50
来自 (保密)
注册日期 2006-11-8
最后登录 2008-7-24 离线

[查看资料]  [发短消息]  [Blog
       
发表于:2008-1-12 17:14 
正在学习中,不怎么看得懂。



网络工程师到底该不该去考CCIE认证?
2008-1-12 17:144楼
[ 顶部 ]
 
ilovefany
新新人类  点击可查看详细



帖子 93
精华 0
无忧币 14
积分 97
阅读权限 20
注册日期 2007-9-16
最后登录 2008-5-23 离线

[查看资料]  [发短消息]  [Blog
       
发表于:2008-1-19 15:59 
学习



网络工程师到底该不该去考CCIE认证?
2008-1-19 15:595楼
[ 顶部 ]
 
linguojin
新新人类  点击可查看详细



帖子 11
精华 0
无忧币 20
积分 25
阅读权限 20
注册日期 2006-11-29
最后登录 2008-5-13 离线

[查看资料]  [发短消息]  [Blog
       
发表于:2008-5-12 14:15 
不懂就是不懂



网络工程师到底该不该去考CCIE认证?
2008-5-12 14:156楼
[ 顶部 ]
 
yorkiey
新新人类  点击可查看详细



帖子 17
精华 0
无忧币 14
积分 17
阅读权限 20
注册日期 2008-4-7
最后登录 2008-7-22 离线

[查看资料]  [发短消息]  [Blog
       
发表于:2008-5-18 14:30 
杭州的吧



网络工程师到底该不该去考CCIE认证?
2008-5-18 14:307楼
[ 顶部 ]
 
klug123
新新人类  点击可查看详细



十二生肖之马   天蝎座   行业勋章   技术勋章   诚信兄弟  
帖子 13
精华 0
无忧币 -2
积分 12
阅读权限 20
注册日期 2008-6-10
最后登录 2008-7-22 离线

[查看资料]  [发短消息]  [Blog
       
发表于:2008-6-10 14:37 
建议想说明 代码 的作用 或者 目的



网络工程师到底该不该去考CCIE认证?
2008-6-10 14:378楼
[ 顶部 ]
     
论坛跳转:  

| | |

| | |

| | |

标记已读 · 删除论坛Cookies · 文本版 · WAP
 
| 诚征版主 | 版主堂 | 意见建议 | 大史记 | 论坛地图
Copyright©2005-2008 51CTO.COM  Powered by Discuz!
本论坛言论纯属发布者个人意见,不代表51CTO网站立场!如有疑义,请与管理员联系。
京ICP备05051492号