公交转车路线sql写法
线路   运行区间和时间   沿途站点    
 游2线下行   灵谷寺公园8:40-17:30 
 雨花台南门7:40-16:30    
 灵谷寺公园-水榭-中山陵-海底世界-明孝陵-石象路-前湖-卫桥(西)-中山门-明故宫-解放路-总统府-大行宫南站-杨公井-夫子庙-三山街-金沙井-长乐路-中华门内-雨花台-雨花新村-共青团路-雨花台南门        
 14路下行   所街5:40-23:00 
 双龙街西站5:10-22:30    
 所街-湖西街(应天大街)-应天大街西-黄山路-泰山路-南苑新村-彩虹苑-集庆门大桥-集庆门-殷高巷-长乐路-箍桶巷-马道街-江宁路-大树城-养虎巷-土城头-土城头南站-城头城-卡子门-夹岗-双龙街西站        
 乘车路线:由   [夫子庙]   坐    <游2线下行>    到   [长乐路]   换乘    <14路下行>    到达   [江宁路]      
 ============================================================================ 
 公交查询,比如从夫子庙到江宁      ,有的可以直达,有的需要转车,按照普通做法似乎要写很多循环来判断,特别是需要转车,请大家提提有没有什么好的写的方法,谢谢,分不够再加
------解决方案--------------------if exists(select * from sysobjects where id=object_id( 'bus_proc ')) 
 drop proc bus_fun 
 GO 
 if exists(select * from sysobjects where id=object_id( 'bus ')) 
 drop table bus 
 GO   
 create table Bus(id int identity(1,1)  --自增ID 
 ,sid int	--车站ID 
 ,lid int)	--公交车ID 
 insert into Bus 
 	  select	1,	1 
 union all select 	3,	1 
 union all select 	5,	1 
 union all select	2,	3 
 union all select	3,	3 
 union all select	4,	5 
 union all select	1,	5 
 union all select	2,	7 
 union all select	4,	7 
 union all select	6,	7 
 union all select	3,	9 
 union all select	1,	9 
 union all select	7,	7  
 union all select	7,	10  
 union all select	12,	10  
 union all select	12,	11  
 union all select	28,	11  
 union all select 	10,	1 
 union all select 	10,	9 
 union all select 	11,	9 
 union all select 	14,	9 
 GO   
 create proc Bus_fun(@sid1 int,@sid2 int,@lev int,@str varchar(8000)= ' ' output) 
 as 
 --BY 华裔大魔王 
 declare @lid int,@buscount int 
 select @lid = 0 , @lev=0 
 select @buscount=count(lid) from (select distinct lid from bus ) tb 
 create table #(Bid int identity(1,1),车站 int,公交 int,lev int) 
 while exists(select 1 from (select distinct 公交 from #) tb having count(1) <@buscount) 
 begin 
 if @lev=0 
 insert into # (车站 ,公交 ,lev ) 
 select sid,lid,@lev from bus a where exists(select * from bus where sid=@sid1 and lid=a.lid) order by lid,sid 
 else 
 insert into # (车站 ,公交 ,lev ) 
 select sid,lid,@lev from bus sb where exists(select * from bus a where exists(select * from # where 车站=a.sid and lev=@lev-1) and not exists (select * from # where 公交=a.lid) and lid = sb.lid) 
 if @@rowcount=0   
 goto ERR	 
 if exists(select * from # a where 车站=@sid2 and lev = @lev)	 
 begin		 
 while @lev> =0 
 begin			 
 select top 1 @lid=公交,@str= '-> 换乘-> 公交 '+cast(公交 as varchar(10))+ '路-> 在 '+cast(车站 as varchar(10)) + '站下车 '+@str from # where 车站=@sid2 and lev=@lev 
 select top 1 @sid2=车站 from # a where lev=@lev-1 and exists(select * from # where lev=@lev and 车站=a.车站 and 公交=@lid) select @lev=@lev-1 
 end				 
 select @str=stuff(@str,1,3, ' ') 
 goto BUSEND 
 end	 
 select @lev=@lev+1 
 end 
 ERR: 
 	select  '无公交线路 ' 
 BUSEND: 
 	drop table # 
 GO   
 set nocount on 
 declare @bus varchar(8000) 
 select @bus= ' ' 
 exec Bus_fun 1,6,0,@bus output 
 if @bus <>  ' ' 
 select @bus     
------解决方案----------------------參考 
 /***********邹老大*************/   
 CREATE TABLE T_Line( 
 ID      nvarchar(10),  --公交线路号 
 Station nvarchar(10),  --站点名称 
 Orders  int)           --行车方向(通过它反应每个站的上一个、下一个站) 
 INSERT T_Line  
 SELECT N '8路 '  ,N '站A