打印本文 打印本文 关闭窗口 关闭窗口
SQL 存储过程&算法
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2775  更新时间:2007/11/14 13:11:30  文章录入:mintao  责任编辑:mintao
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.id<b.id
and 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<b.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<b.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<b.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 ''''文一路口'''',''''总管塘''''

上一页  [1] [2] 

打印本文 打印本文 关闭窗口 关闭窗口