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

China☆狼群 提交于 2019-12-13 02:56:41

问题


I am migrating a process from oracle to postgresql, and I am in another problem with the conversion of them.

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"

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.

This is the query in 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

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.


回答1:


Yes, I have also used "Connect by prior" conversion in Postgresql using "With Recursive" queries And I find this is the right approach.

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:

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;


来源:https://stackoverflow.com/questions/49537740/migrate-query-with-start-with-and-connect-by-prior-from-oracle-to-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!