如何将使用MySQL变量的查询转换为Oracle?

时间:2011-12-05 16:31:14

标签: mysql oracle10g

我为MySQL写了这个查询。如何使用变量@SA和@SB转换部件以使用oracle 10g?

提前谢谢你。

select ID1,ID2, PID, SEQ1, KEY1, NAME1, DESC1, STA1, AS1, SEQ2, KEY2, NAME2, DESC2, STA2, AS2, 
case when ( SORT is not null ) then @SA := SORT else @SA := @SA + 1 end as S1 , 
case when ( SORT2 is not null ) then @SB := SORT2 else @SB := @SB + 1 end as S2   
from ( select @SA := 400 , doc.ID ID1, dn.SEQUENCE SEQ1, 
doc.DOCUMENTKEY KEY1, doc.NAME NAME1,  
doc.DESCRIPTION DESC1, doc.STATUSID STA1, 
doc.ASSIGNEDTOID AS1, doc.PROJECTID  PID, 
v.ORIGINDOCUMENTID ORG1, dn.GLOBALSORTORDER SORT   
from DOCUMENT doc inner join DOCUMENTNODE dn on doc.ID = dn.REFID 
inner join BASELINE b on dn.BASELINEID = b.ID 
inner join VERSION v on  v.DOCUMENTID = dn.REFID 
where dn.ACTIVE = 'T' 
and doc.PROJECTID = b.PROJECTID 
and dn.BASELINEID = 9   
and dn.SEQUENCE like '2%' ) b1
LEFT JOIN
( select @SB := 200 , doc.ID ID2,  dn.SEQUENCE SEQ2, doc.DOCUMENTKEY KEY2, doc.NAME NAME2, doc.DESCRIPTION DESC2, doc.STATUSID STA2, doc.aSSIGNEDTOID AS2, 
v.ORIGINDOCUMENTID ORG2, dn.GLOBALSORTORDER SORT2  
from dOCUMENT doc inner join DOCUMENTNODE dn on doc.ID = dn.REFID 
inner join BASELINE b on dn.BASELINEID = b.id 
inner join VERSION v on  v.DOCUMENTID = dn.REFID 
where dn.ACTIVE = 'T' and doc.PROJECTID = b.PROJECTID 
   and dn.BASELINEID = 10     
 and dn.SEQUENCE like '3.%' ) b2
  on b1.ORG1 = b2.ORG2 
  ORDER BY S2, S1 

1 个答案:

答案 0 :(得分:0)

Oracle以稍微不同的方式处理变量/重命名列。您只需对计算出的表达式使用“AS”关键字,并为其命名,如上所述。

例如,

select @SA := 400 , doc.ID ID1, dn.SEQUENCE SEQ1 from document1

将更改为

select 400 AS SA, doc.ID ID1, dn.SEQUENCE SEQ1 from document1

提供新名称后,您可以在所有封闭查询中使用该名称,如下所示。

SQL> select empno, sal, 20 as bonus_percent from scott.emp;

     EMPNO        SAL BONUS_PERCENT

      7369        800            20
      7499       1600            20
      ......

SQL> select empno, sal* (100+bonus_percent)/100 from (
  2     select empno, sal, 20 as bonus_percent from scott.emp
  3  );

     EMPNO SAL*(100+BONUS_PERCENT)/100
---------- ---------------------------
      7369                         960
      7499                        1920
      .....