如何将使用变量的查询从 MySQL 转换为 Oracle?
我为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Oracle 处理变量/重命名列的方式略有不同。您可以只使用“AS”关键字来计算表达式,并像上面那样给它们命名。
例如,
将更改为
一旦您给出新名称,您就可以在所有封闭的查询中使用该名称,如下所示。
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,
will change to
Once you give a new name, you can use that name in all the enclosing queries as below.