Hi,
I have the below logic.My doubt is
Should I have the control in the second part of the UNION ALL statement to end the recursive logic or automatically it gets stopped once there is no condition satisfies the logic?
CREATE TABLE TABLE AS (
WITH RECURSIVE TABLE_G (MERGE_ID, REM_ID, DW_EFF_DT, FIRST_MERGE_DT, CURR_MERGE_DT, HIER,LEVL)
AS
--SEED QUERY
(SEL MERGE_ID,MERGE_ID, DW_EFF_DT, FIRST_MERGE_DT, CURR_MERGE_DT,CAST('\' AS VARCHAR(100)) AS HIER,CAST(1 AS DECIMAL(32,0)) AS LEVL FROM MERGE_T
WHERE MERGE_ID=0
UNION ALL
SEL A.MERGE_ID,B.REM_ID, B.DW_EFF_DT, A.FIRST_MERGE_DT,
CASE WHEN B.CURR_MERGE_DT= '9999-12-31' THEN A.CURR_MERGE_DT ELSE B.CURR_MERGE_DT END AS
CURR_MERGE_DT,
B.HIERARCHY||B.MERGE_ID||'\' AS HIER, B.LEVL+1
FROM MERGE_T A INNER JOIN TABLE_G B
ON A.REM_ID = B.MERGE_ID
)
SELECT * FROM TABLE_G
) WITH DATA
Forums: