2017-07-25 64 views
1

我正在尋找將兩個表加入臨時表,然後在選擇查詢中使用臨時表。這是我想要創建的臨時表的select語句:SQL - 創建加入的臨時表

select program, event 
from OMEGA.HP 
inner join POM.GT 
on program = substring(name,7,4) 
where LENGTH(name)= 25 

什麼是爲上述創建臨時表的最簡單方法?

回答

1
select program, event 
into #temp --------------------------------check here 
from OMEGA.HP 
inner join POM.GT 
on program = substring(name,7,4) 
where LENGTH(name)= 25 
0

您可以創建一個臨時表(和你可能想),但如果你不知道的,我想指出,你可以在這樣

查詢創建一個虛擬的臨時表
SELECT * 
FROM (
    select program, event 
    from OMEGA.HP 
    inner join POM.GT on program = substring(name,7,4) 
    where LENGTH(name)= 25 
) AS Virtual_table 

,你甚至可以加入到另一個這些表是這樣的:

SELECT * 
FROM (
    select program, event 
    from OMEGA.HP 
    inner join POM.GT on program = substring(name,7,4) 
    where LENGTH(name)= 25 
) AS v_table74 
join (
    select program, event 
    from OMEGA.HP 
    inner join POM.GT on program = substring(name,2,5) 
    where LENGTH(name)= 25 
) as v_table25 on v_table74.program = v_table25.program 
0

使用公用表表達式(CTE):

with temptablename(program, event) as (
    select program, event 
    from OMEGA.HP 
    inner join POM.GT 
    on program = substring(name,7,4) 
    where LENGTH(name)= 25 
) 
select program, event 
from temptablename;