我需要在以下代码中将p3.ID_miasta分配给@tabela,以便p3中的ID_miasta存储在表变量@tabela中。它说语法不正确附近' ='当我写@ tabela = p3.ID_miasta。你知道我能做到的其他方式吗?
create type cat as table
(id int)
declare @tabela cat;
with droga as
(
select
@tabela = p3.ID_miasta,
p2.ID_miasta koniec,
p1.ID_pociagu ostatni,
',' + cast(@poczatek as varchar(max)) + ',' + cast(p2.ID_miasta as varchar(max)) + ',' przesiadki,
',' + cast(p1.ID_pociagu as varchar(max)) + ',' pociagi
from
postoje p1
inner join
postoje p2 on (p1.ID_pociagu = p2.ID_pociagu
and p1.ID_miasta = @poczatek
and p2.ID_miasta <> @poczatek)
inner join
postoje p3 on (p1.ID_pociagu = p3.ID_pociagu
and (p3.odleglosc between (p1.odleglosc) and (p2.odleglosc)))
union all
-- (something not important)
)
答案 0 :(得分:1)
两个CTE怎么样?而不是表变量将查询包装在CTE中并在第二个查询中使用它。
with tabela as
(
select
p3.ID_miasta,
p2.ID_miasta koniec,
p1.ID_pociagu ostatni,
',' + cast(@poczatek as varchar(max)) + ',' + cast(p2.ID_miasta as varchar(max)) + ',' przesiadki,
',' + cast(p1.ID_pociagu as varchar(max)) + ',' pociagi
from
postoje p1
inner join
postoje p2 on (p1.ID_pociagu = p2.ID_pociagu
and p1.ID_miasta = @poczatek
and p2.ID_miasta <> @poczatek)
inner join
postoje p3 on (p1.ID_pociagu = p3.ID_pociagu
and (p3.odleglosc between (p1.odleglosc) and (p2.odleglosc)))
)
, droga as
(
select * from tabela
union all
-- another select using tabela
)