2014-01-29 12 views
3

將標準SQL連接轉換爲舊的Oracle連接語法時出現問題(請不要問我爲什麼需要這樣做)。我希望同樣的結果,無論如何,它是不是:從標準SQL到Oracle的轉換語法

樣本數據:

create table testing (
aid number(8), 
bid number(8), 
btext varchar(80)); 

insert into testing values (100,1,'text1a'); 
insert into testing values (100,2,'text1b'); 
insert into testing values (100,3,'text1c'); 
insert into testing values (200,19,'text2b'); 
insert into testing values (200,18,'text2a'); 
insert into testing values (300,4324,'text3a'); 
insert into testing values (500,80,'text4a'); 
insert into testing values (50,2000,'text5a'); 
commit; 

標準SQL:

select a.*,b.* from testing a 
left outer join testing b 
on (a.aid = b.aid and a.bid < b.bid) 
order by a.aid, b.bid; 

AID BID BTEXT AID_1 BID_1 BTEXT_1 
50 200 text5a NULL NULL NULL 
100 1 text1a 100  2  text1b 
100 2 text1b 100  3  text1c 
100 1 text1a 100  3  text1c 
100 3 text1c NULL NULL NULL 
200 18 text2a 200  19  text2b 
200 19 text2b NULL NULL NULL 
300 432 text3a NULL NULL NULL 
500 80 text4a NULL NULL NULL 

的Oracle SQL:

select a.*,b.* from testing a, testing b 
where a.aid = b.aid(+) 
and a.bid < b.bid 
order by a.aid, b.bid; 

AID BID BTEXT AID_1 BID_1 BTEXT_1 
100 1 text1a 100  2  text1b 
100 2 text1b 100  3  text1c 
100 1 text1a 100  3  text1c 
200 18 text2a 200  19  text2b 

如何獲得使用Oracle的傳統語法的標準SQL的相同結果?

+1

爲什麼要使用傳統的語法?我知道你寫了不要問,但...使用ANSI標準,你不需要轉換任何東西。 – Ben

+0

有些公司(甚至是大公司)因爲歷史原因迫使使用舊的語法,即使在使用Oracle 12c時也是如此。我也不喜歡它... – royskatt

+0

如果你想用'FAST REFRESH'創建'MATERIALIZED VIEW',你必須使用舊的Oracle語法,ANSI不起作用。即使在版本12c中,Oracle也不認爲這是一個錯誤,而只是「缺少文檔」! –

回答

3

您的Oracle風格語句也需要(+)運算符處於低於條件,因爲這也是標準SQL版本中連接條件的一部分。

select a.*,b.* from testing a, testing b 
where a.aid = b.aid(+) 
and a.bid < b.bid(+) 
order by a.aid, b.bid; 

See sqlfiddle here.

+0

非常感謝,也爲鏈接! – royskatt