读书人

怎样写列车查询车次的存储过程?帮忙!

发布时间: 2012-01-26 19:40:46 作者: rapoo

怎样写列车查询车次的存储过程??帮忙!
有两张表,一张车次表(表:A),一张车次站名表(表:B),两表存在主外键关系

表A:
Code(车次主键) FC(起点站) TC(终到站) LT(开车时间) AT(到站时间) Dis(里程)
T2 广州    北京    5:00 20:00 1800
T3 北京    广州    5:00 20:00 1800
Y2 兰州    厦门    5:00 20:00 1200
Y3 厦门    兰州    5:00 20:00 1200

表B:
Id(主键) Code(车次外键) CN(站名) SN(站号) AT(开车) LT(到站时) Dis(里程)
1 T2 广州 1 5:00 null 0
2 T2 株洲 2 8:05 8:00 360
3 T2 武汉 3 11:05 11:00 360
4 T2 郑州 4 14:05 14:00 360
5 T2 石家庄 5 17:05 17:00 360
6 T2 北京 6 null 20:00 360

7 T3 北京 1 5:00 null 0


8 T3 石家庄 2 8:05 8:00 360
9 T3 郑州 3 11:05 11:00 360
10 T3 武汉 4 14:05 14:00 360
11 T3 株洲 5 17:05 17:00 360
12 T3 广州 6 null 20:00 360

13 Y2 兰州 1 5:00 null 0
14 Y2 西安 2 8:05 8:00 240
15 Y2 襄樊 3 11:05 11:00 240
16 Y2 武汉 4 14:05 14:00 240
17 Y2 南昌 5 17:05 17:00 240


18 Y2 厦门 6 null 20:00 240

19 Y3 厦门 1 5:00 null 0
20 Y3 南昌 2 8:05 8:00 240
21 Y3 武汉 3 11:05 11:00 240
22 Y3 襄樊 4 14:05 14:00 240
23 Y3 西安 5 17:05 17:00 240
24 Y3 兰州 6 null 20:00 240


现在输入上车车站名(例如:株洲)和要到达的站名(例如:西安),两站没有直达车次,还有考虑方向问题,最终显示这行程所途径的站名,里程
这样要写存储过程,应该怎样写呢?
各位大哥大姐!帮帮小弟!(有分送)

[解决办法]
直接摆渡一下,找现成的代码研究吧
[解决办法]
给个类似,参考一下吧`

CREATE TABLE T_Line(
ID nvarchar(10), --公交线路号
Station nvarchar(10), --站点名称
Orders int) --行车方向(通过它反应每个站的上一个、下一个站)
INSERT T_Line
SELECT N '8路 ' ,N '站A ',1 UNION ALL
SELECT N '8路 ' ,N '站B ',2 UNION ALL
SELECT N '8路 ' ,N '站C ',3 UNION ALL
SELECT N '8路 ' ,N '站D ',4 UNION ALL
SELECT N '8路 ' ,N '站J ',5 UNION ALL
SELECT N '8路 ' ,N '站L ',6 UNION ALL
SELECT N '8路 ' ,N '站M ',7 UNION ALL
SELECT N '20路 ' ,N '站G ',1 UNION ALL
SELECT N '20路 ' ,N '站H ',2 UNION ALL
SELECT N '20路 ' ,N '站I ',3 UNION ALL


SELECT N '20路 ' ,N '站J ',4 UNION ALL
SELECT N '20路 ' ,N '站L ',5 UNION ALL
SELECT N '20路 ' ,N '站M ',6 UNION ALL
SELECT N '255路 ',N '站N ',1 UNION ALL
SELECT N '255路 ',N '站O ',2 UNION ALL
SELECT N '255路 ',N '站P ',3 UNION ALL
SELECT N '255路 ',N '站Q ',4 UNION ALL
SELECT N '255路 ',N '站J ',5 UNION ALL
SELECT N '255路 ',N '站D ',6 UNION ALL
SELECT N '255路 ',N '站E ',7 UNION ALL
SELECT N '255路 ',N '站F ',8
GO

--乘车线路查询存储过程
CREATE PROC p_qry
@Station_Start nvarchar(10),
@Station_Stop nvarchar(10)
AS
SET NOCOUNT ON
DECLARE @l int
SET @l=0
SELECT ID,Station,
Line=CAST( '( '+RTRIM(ID)+ ': '+RTRIM(Station) as nvarchar(4000)),
Orders=Orders,
[Level]=@l
INTO # FROM T_Line
WHERE Station=@Station_Start
WHILE @@ROWCOUNT> 0
AND NOT EXISTS(SELECT * FROM # WHERE Station=@Station_Stop)
BEGIN
SET @l=@l+1
INSERT #(Line,ID,Station,Orders,[Level])
SELECT
Line=a.Line+CASE
WHEN a.ID=b.ID THEN N '-> '+RTRIM(b.Station)
ELSE N ') ∝ ( '+RTRIM(b.ID)
+N ': '+RTRIM(b.Station) END,
b.ID,b.Station,b.Orders,@l
FROM # a,T_Line b
WHERE a.[Level]=@l-1
AND(a.Station=b.Station AND a.ID <> b.ID
OR a.ID=b.ID AND(
a.Orders=b.Orders+1
OR
a.Orders=b.Orders-1))
AND LEN(a.Line) <4000
AND PATINDEX( '%[ > ] '+b.Station+ '[-)]% ',a.Line)=0
END
SELECT N '起点站 '=@Station_Start
,N '终点站 '=@Station_Stop
,N '乘车线路 '=Line+N ') '
FROM #
WHERE [Level]=@l
AND Station=@Station_Stop
IF @@ROWCOUNT =0 --如果未有可以到达的线路,则显示处理结果表备查
SELECT * FROM #
GO

--调用
EXEC p_qry N '站A ',N '站L '
/*--结果
起点站 终点站 乘车线路
---------- ------------ -----------------------
站A 站L (8路: 站A-> 站B-> 站C-> 站D-> 站J-> 站L)
--*/

[解决办法]

create table checi
(
Code varchar(20),
FC nvarchar(20),
TC nvarchar(20),
LT varchar(20),
AT varchar(20),
Dis int
)
go

insert into checi
select 'T2 ', '广州 ', '北京 ', '5:00 ', '20:00 ',1800
union all select 'T3 ', '北京 ', '广州 ', '5:00 ', '20:00 ',1800
union all select 'Y2 ', '兰州 ', '厦门 ', '5:00 ', '20:00 ',1200
union all select 'Y3 ', '厦门 ', '兰州 ', '5:00 ', '20:00 ',1200
go

create table chezhan
(
Id int identity(1,1),
Code varchar(20),
CN nvarchar(20),
SN int,
AT varchar(20),
LT varchar(20),
Dis int
)
go

select * from chezhan
insert into chezhan
select 'T2 ', '广州 ',1, '5:00 ',null,0
union all select 'T2 ', '株洲 ',2, '8:05 ', '8:00 ',360
union all select 'T2 ', '武汉 ',3, '11:05 ', '11:00 ',360
union all select 'T2 ', '郑州 ',4, '14:05 ', '14:00 ',360
union all select 'T2 ', '石家庄 ',5, '17:05 ', '17:00 ',360
union all select 'T2 ', '北京 ',6,null, '20:00 ',360
union all select 'T3 ', '北京 ',1, '5:00 ',null,0


union all select 'T3 ', '石家庄 ',2, '8:05 ', '8:00 ',360
union all select 'T3 ', '郑州 ',3, '11:05 ', '11:00 ',360
union all select 'T3 ', '武汉 ',4, '14:05 ', '14:00 ',360
union all select 'T3 ', '株洲 ',5, '17:05 ', '17:00 ',360
union all select 'T3 ', '广州 ',6,null, '20:00 ',360
union all select 'Y2 ', '兰州 ',1, '5:00 ',null,0
union all select 'Y2 ', '西安 ',2, '8:05 ', '8:00 ',240
union all select 'Y2 ', '襄樊 ',3, '11:05 ', '11:00 ',240
union all select 'Y2 ', '武汉 ',4, '14:05 ', '14:00 ',240
union all select 'Y2 ', '南昌 ',5, '17:05 ', '17:00 ',240
union all select 'Y2 ', '厦门 ',6,null, '20:00 ',240
union all select 'Y3 ', '厦门 ',1, '5:00 ',null,0
union all select 'Y3 ', '南昌 ',2, '8:05 ', '8:00 ',240
union all select 'Y3 ', '武汉 ',3, '11:05 ', '11:00 ',240
union all select 'Y3 ', '襄樊 ',4, '14:05 ', '14:00 ',240
union all select 'Y3 ', '西安 ',5, '17:05 ', '17:00 ',240
union all select 'Y3 ', '兰州 ',6,null, '20:00 ',240


create proc up_GetCheCiInfo
(
@Begin nvarchar(20),
@End nvarchar(20)
)
as
begin

select c.code as 车次,c.FC as 始发站,c.TC as 终点站,
temp.CN 站名 ,temp.AT as 到站时间,Temp.LT as 离站时间,temp.Dis 中途距离,SN
from checi c
inner join
(
select z.* from chezhan z
inner join
(
select b.Code,b.SN as beginSn,e.SN as EndSN
from chezhan b
inner join chezhan e
on b.Code = e.Code and b.SN < e.SN and e.CN = @End
where b.CN = @Begin
) temp
on z.code = temp.code and z.SN > = beginSN and z.SN <= EndSN
) temp
on temp.Code = c.Code
order by 车次,SN
end

exec up_GetCheCiInfo '武汉 ', '西安 '

结果为:

车次 始发站 终点站 站名 到站时间 离站时间 中途距离 SN
Y3厦门兰州武汉11:0511:002403
Y3厦门兰州襄樊14:0514:002404
Y3厦门兰州西安17:0517:002405
[解决办法]
终于整出来了

CREATE PROCEDURE dbo.aes_Code
@fc nvarchar(20),
@tc nvarchar(20)
AS
begin
declare @Code nvarchar(20)
set @Code= ' '
select @Code=b.Code from b inner join b a on a.code=b.code
where b.cn=@fc and a.cn=@tc

if @Code <> ' '
begin

select *
from b tem
where ID > =( select ID from b where cn=@fc and Code=tem.Code) and ID <=( select ID from b where Code=tem.Code and cn=@tc)
order by Code
end
else
begin
declare @S nvarchar(20)
declare @E nvarchar(20)
select t.ID as SID, T.CODE AS SCode,t.CN ,y.ID AS EID,Y.CODE AS ECode
into #aa
from ( select *
from b tem
where ID > =( select ID from b where cn=@fc and Code=tem.Code) ) t
inner join
( select *
from b tem
where ID <=( select ID from b where Code=tem.Code and cn=@tc))y
on t.cn=y.cn

select tem.*
from b tem inner join #AA on tem.code=#aa.scode
where ID > =( select ID from b where cn=@fc and Code=tem.Code) and ID <=#aa.sid


union
select tem.*
from b tem inner join #AA on tem.code=#aa.ecode
where ID <=( select ID from b where Code=tem.Code and cn=@tc) and ID> =#aa.eid
end
end
GO


--
aes_Code N '株洲 ',N '西安 '
--结果
2T2株洲28:058:00360
3T2武汉311:0511:00360
21Y3武汉311:0511:00240
22Y3襄樊414:0514:00240
23Y3西安517:0517:00240

aes_Code N '武汉 ',N '西安 '
--j结果
21Y3武汉311:0511:00240
22Y3襄樊414:0514:00240
23Y3西安517:0517:00240


读书人网 >SQL Server

热点推荐