方式一
with TEST(ms,end_month) as (
select CAST( to_date('2022-01-01','yyyy-MM-dd') as date) ms, CAST(to_date( '2023-07-01','yyyy-MM-dd') as date) end_month FROM DUAL
UNION ALL
SELECT CAST(to_date(to_char(ADD_MONTHS(ms,1),'yyyy-MM-dd'),'yyyy-MM-dd')as date) ms, cast(end_month as date) FROM TEST
WHERE ADD_MONTHS(ms,1) <= end_month
)
SELECT ms FROM TEST
注:在oracle11的部分版本中,该方法可能存在bug
方式二
SELECT TRUNC (ADD_MONTHS((TO_DATE('02-2022', 'MM-YYYY')), ROWNUM -1)) dt
FROM DUAL
CONNECT BY ROWNUM <= MONTHS_BETWEEN( TO_DATE('02-2023', 'MM-YYYY') , (TO_DATE('02-2022', 'MM-YYYY')))+1