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

发布于 2024-12-19 15:06:17 字数 1420 浏览 0 评论 0原文

我为 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 

I wrote this query for MySQL. How can I convert the part with variable @SA and @SB to work with oracle 10g?

Thanks you in advance.

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2024-12-26 15:06:17

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
      .....

Oracle deals with variables/renaming columns a little differently. You can just use the "AS" keyword for calculated expressions and give them a name as you did above.

For example,

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

will change to

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

Once you give a new name, you can use that name in all the enclosing queries as below.

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
      .....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文