How to populate calendar table in Oracle?

I want to maintain a calender table in Oracle DB which I want to populate with all the days of the year starting from 2011 to 2013 (it may be till any year). How can I do that?

Consider my DB table has columns and example dataset is:

S.No  Cal_Dt      DayName 
1     01-01-2011  Monday
2     02-01-2011  Tuesday
3     03-01-2011  Wednesday

and so on.

I am more concerned with the Cal_Dt only here (DayName is optional).


Solution 1:

This is a simple and easy way to do it

with calendar as (
        select :startdate + rownum - 1 as day
        from dual
        connect by rownum < :enddate - :startdate
    )
select rownum as "S.No", to_date(day,'dd_mm_yyyy') as "Cal_Dt", to_char(day,'day') as "DayName"
from calendar

Solution 2:

with calendar as (
        select rownum - 1 as daynum
        from dual
        connect by rownum < sysdate - to_date('1-jan-2010') + 1
    )
select to_date('1-jan-2010') + daynum as monthdate
from calendar
;

Solution 3:

declare
  v_date date := to_date('20110101','yyyymmdd');
begin

   while v_date < sysdate + 720 loop

      insert into calender
      values ( v_date, to_char(v_date,'DAY'));

      v_date := v_date + 1;

   end loop;
   commit;

end;
/

This is not best practice and you should use Allesandro Rossi's solution. This may only be useful if you're using Oracle 9i or earlier and populating a large table.