表结构如下:
第一个表
id busname svctime stoe line
   x路车  发车和收车时间 起始站点和终点 该路车所有站点
第2个表
idx(序号)   station站点
存储过程为:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[busSearch]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[busSearch]
GOSET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GOCREATE    proc busSearch
@sta1 nvarchar(50), @sta2 nvarchar(50)
as
create table #tmp1(
r_busname nvarchar(100),
r_st char(30),
r_stoe nvarchar(200),
r_line nvarchar(1200)
)
create table #tmp2(
r_busname nvarchar(100),
r_st char(30),
r_stoe nvarchar(200),
r_line nvarchar(1200)
)
--查找结果
create table #result(
r_bn1 nvarchar(100),
r_st1 char(30),
r_stoe1 nvarchar(200),
r_line1 nvarchar(1200),
r_station nvarchar(100),
r_bn2 nvarchar(100),
r_st2 char(30),
r_stoe2 nvarchar(200),
r_line2 nvarchar(1200)
)
set @sta1=@sta1+'→'
set @sta2=@sta2+'→'
--直达
insert into #result(r_bn1,r_st1,r_stoe1,r_line1)
select a.busname,a.svctime,a.stoe,rtrim(a.line) 
from tb_kmbusline a
where charindex(@sta1,a.line)>0 and  charindex(@sta1,a.line)<charindex(@sta2,a.line)
if @@rowcount<=0 begin
insert into #tmp1(r_busname,r_st,r_stoe,r_line)
select a.busname,a.svctime,a.stoe,rtrim(a.line) from tb_kmbusline a
where charindex(@sta1,a.line)>0
insert into #tmp2(r_busname,r_st,r_stoe,r_line)
select a.busname,a.svctime,a.stoe,rtrim(a.line) from tb_kmbusline a
where charindex(@sta2,a.line)>0
insert into #result
select a.r_busname,a.r_st,a.r_stoe,a.r_line, c.station, b.r_busname,b.r_st,b.r_stoe,b.r_line
from #tmp1 a, #tmp2 b, tb_kmbusstations c
where c.station+'→'<>@sta1 and c.station+'→'<>@sta2
and charindex(c.station+'→',a.r_line)>0 and charindex(c.station+'→',b.r_line)>0
and charindex(@sta1,a.r_line)<charindex(c.station+'→',a.r_line)
and charindex(@sta2,b.r_line)>charindex(c.station+'→',b.r_line)
if @@rowcount<=0 begin
insert into #result(r_bn1,r_st1,r_stoe1,r_line1,r_station,r_bn2,r_st2,r_stoe2,r_line2)
select a.r_busname,a.r_st,a.r_stoe,a.r_line,'',b.r_busname,b.r_st,b.r_stoe,b.r_line
from #tmp1 a, #tmp2 b
end   -- */
end
select * from #result
drop table #result
drop table #tmp1
drop table #tmp2
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
问题:怎么优化可以实现转乘一目了然和查询速度快?如下图效果: