2013-07-25 74 views
1

如何執行使用由WITH準備的數據的多個語句。 例如:WITH oracle中的多個語句

WITH t1 AS 
(
....using some table 
), 
t2 as 
(
....using t1 and some other tables 
), 
t3 as 
(
..using t1 and t2 and some other tables 
) 
statement 1; (let say this is using t1 and t2) 
statement 2; (let say this is using t2 and t3) 

如何在Oracle中執行此操作?

+1

WITH子句是聲明的一部分。你在尋找類似VIEWs的東西嗎?或者也許是臨時表?或PLSQL集合? –

+0

不以這種方式工作 –

+0

WITH是SQL的一部分。 SQL沒有「可執行語句」。 –

回答

0

例如:

-- Block With 
-------------------------------------------------------------------------------- 
with 
w1 as 
(
select 1 as id 
     , '123' as text 
from dual 
) 

, w2 as 
(
select w1.* 
from w1 
) 

, w3 as 
(
select w2.* 
from w2 
) 
-- End Block With 
-------------------------------------------------------------------------------- 

select * from w1, w2, w3; 
+0

如果我想在'select * from w1,w2,w3;' –

+0

之後添加'select * from w1'這樣的其他語句,我認爲這是不可能的。您只能在一個語句中使用'WITH'。你需要得到什麼?也許有另一種方式。 –

2

甲子查詢分解條款(WITH子句)是一個單一的查詢,在那裏它有效地充當單次使用的視圖的一部分。如果您發現需要在多個查詢的WITH子句中重複SELECT語句,則可能需要考慮在子查詢因子分解子句中爲每個SELECT定義視圖以簡化代碼。分享並享受。

0

你可以子句中使用下面給出的以用於多臺積木

WITH 
t1 
AS (
     SELECT column1, column2 
     FROM some_tables     
      ), 
t2 as 
(
     SELECT column4,column4 
     FROM some tables 
)     
SELECT column5,column6 --USE ALIAS OF THE ABOVE BLOCKS YOU HAVE CREATED DOWN AND USE THEM AND SELECT THE IN BASE QUERY 
      FROM 
      (
       SELECT column1 COLUMN 5,column2 COLUMN 6 
       FROM t1 a 
       union ALL 
       SELECT column3 COLUMN 5,column4 COLUMN 6 
       FROM t2 a 

       ) 

希望能幫助。