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

發佈了80 篇原創文章 · 獲贊 0 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章