简体   繁体   English

使用START WITH和CONNECT BY PRIOR将查询从Oracle迁移到PostgreSQL

[英]Migrate query with START WITH and CONNECT BY PRIOR from oracle to postgresql

I am migrating a process from oracle to postgresql, and I am in another problem with the conversion of them. 我正在将一个进程从oracle迁移到postgresql,而它们的转换又遇到了另一个问题。

I have been researching how to migrate an oracle query, which has "START WITH" and "CONNECT BY PRIOR", I have documented with respect to this, and I think the easiest way to do it is with "WITH RECURSIVE" 我一直在研究如何迁移具有“ START WITH”和“ CONNECT BY PRIOR”的oracle查询,对此我已经进行了文档记录,我认为最简单的方法是使用“ WITH RECURSIVE”

Make the migration of the query, but I'm not sure about the results they throw since the bd oracle and postgres are different, and it is not possible to homologate the bd. 进行查询的迁移,但是由于bd oracle和postgres不同,因此我不确定它们抛出的结果,因此无法对bd进行认证。

This is the query in Oracle 这是Oracle中的查询

SELECT edef_codigo, etdf_transac, edef_detail--, LEVEL
FROM edeft
WHERE edef_distrib in('OM', 'N/A')
AND  pers_codigo_socadm = 311745439
AND  ctac_correlativo = 7513
START WITH etdf_transac = 'SDN'
CONNECT BY PRIOR edef_codigo = edef_padre;

And this is the query in postgresql 这是PostgreSQL中的查询

WITH RECURSIVE edf AS ( SELECT ed.edef_codigo, ed.etdf_transac, 
                               ed.edef_detail
                        FROM edeft ed
                        WHERE ed.edef_distrib in('OM', 'N/A')
                        AND  ed.pers_codigo_socadm = 311745439
                        AND  ed.ctac_correlativo = 7513
                        AND ed.etdf_transac = 'SDN'

                        UNION ALL

                        SELECT ed.edef_codigo, ed.etdf_transac, 
                               ed.edef_detail
                        FROM edeft ed
                             JOIN edf ON edf.edef_codigo = ed.edef_padre    
                        WHERE ed.edef_distrib in('OM', 'N/A')
                        AND  ed.pers_codigo_socadm = 311745439
                        AND  ed.ctac_correlativo = 7513                 
                      )
                    SELECT * FROM edf;

I am still new to postgres and this consultation has made me especially complicated, since I have not found examples similar to what I have. 我对Postgres还是陌生的,这次咨询使我特别复杂,因为我没有找到与我相似的例子。

Yes, I have also used "Connect by prior" conversion in Postgresql using "With Recursive" queries And I find this is the right approach. 是的,我还使用“带递归”查询在Postgresql中使用了“以前连接”转换,我发现这是正确的方法。

One simple example in reference to connect by prior: 参考一个简单的示例,以按先连接:

Oracle: 甲骨文:

Select name, age from user_test connect by prior user_id=parent_id start with user_id='a';

Postgres: Postgres:

with recursive cte_name as 
(select u1.name, u1.user_id, u1.age from user_test u1 where user_id='a' 
UNION ALL select u2.name, u2.user_id, u2.age from user_test u2 
join cte_name on cte_name.user_id=u2.parent_id) select name,age from cte_name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM