34,873
社区成员
发帖
与我相关
我的任务
分享--存储过程A
CREATE proc dbo.getHC_0
(@start_stop int,@end_stop int)
as
begin
declare @e_stop_name varchar(50)
set @e_stop_name=dbo.getStopName(@end_stop)
select top 5 A.bus_id,e_stop=@e_stop_name,num=ABS(A.req-B.req) into #t from (select bus_id,req from Chengdu_BSR where stop_id = @start_stop)A,
(select bus_id,req from Chengdu_BSR where stop_id = @end_stop)B
where A.bus_id = B.bus_id order by num
select bus_name,e_stop,num from #t,Chengdu_bus where #t.bus_id=Chengdu_bus.bus_id
end
GO存储过程B
CREATE proc getHC_1
(@start_stop varchar(50),@end_stop varchar(50))
as
begin
declare @t table(m_state varchar(50),e_state varchar(50),bus_name1 varchar(20),bus_name2 varchar(20),num1 int,num2 int)--总路线表
declare @temp_stop int --中转站ID
declare @s_stop int
declare @e_stop int
set @s_stop=dbo.getStopId(@start_stop)
set @e_stop=dbo.getStopId(@end_stop)
print @s_stop
print @e_stop
--得到中转站表#t
select A.stop_id into #t from(select distinct stop_id from Chengdu_BSR where bus_id in
(select bus_id from Chengdu_BSR where stop_id = @s_stop))A,
(select distinct stop_id from Chengdu_BSR where bus_id in
(select bus_id from Chengdu_BSR where stop_id = @e_stop))B where A.stop_id= B.stop_id
--使用游标遍历中转站表#t
create table #t1(bus_name varchar(20),e_stop varchar(50),num int)
create table #t2(bus_name varchar(20),e_stop varchar(50),num int)
declare mstop_cursor cursor for select * from #t
open mstop_cursor
fetch next from mstop_cursor into @temp_stop
while @@fetch_status=0
begin
truncate table #t1
truncate table #t2
insert into #t1 exec dbo.getHC_0 @s_stop,@temp_stop --返回bus_id,e_stop,num并填充进#t1
insert into #t2 exec dbo.getHC_0 @temp_stop,@e_stop --返回bus_id,e_stop,num并填充进#t2
insert @t select #t1.e_stop as m_state,#t2.e_stop as e_state,#t1.bus_name as bus_name1,#t2.bus_name as bus_name2,#t1.num as num1,#t2.num as num2
from #t1 cross join #t2 --返回通过此个中转站的所有可能路线
fetch next from mstop_cursor into @temp_stop
end
close mstop_cursor
deallocate mstop_cursor
select top 12 start_stop=@start_stop,end_stop=e_state,m_stop=m_state,bus_name1,bus_name2,total_stops=num1+num2 into #x from @t
order by total_stops
select identity(int,1,1)as line_id,start_stop,end_stop,m_stop,bus_name1,bus_name2,total_stops into #Y from #x
select * from #Y
end
GOset nocount on
create table tb(id int)
insert tb select 2
go
create proc a
as
begin
select id,a ='a' into #t from tb
select id,a from #t
end
go
create proc b
as
begin
create table #(id int,c char(1))
select id,b='b' into #t from tb
exec a
end
go
b
go
drop table tb
go
drop proc a
go
drop proc b
/*
服务器: 消息 207,级别 16,状态 3,过程 a,行 5
列名 'a' 无效。
*/set nocount on
create table tb(id int)
insert tb select 2
go
create proc a
as
begin
select id,a ='a' into #t from tb
exec('select id,a from #t')
end
go
create proc b
as
begin
select id,b='b' into #t from tb
exec a
end
go
b
go
drop table tb
go
drop proc a
go
drop proc b
/*
id a
----------- ----
2 a
*/