Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Dates and Loops question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-10-03, 16:53
klecks21 klecks21 is offline
Registered User
 
Join Date: Apr 2003
Posts: 5
Dates and Loops question

I have a table called Months_Days and I want to fill it with the months 1 thru 12 in the months column, and fill in the corresponding number of days in the days column. i only want to use 1 loop and 1 Insert Into statement. Here's what i have so far. i can get the months inserted, but it inserts 31 for the number of days for each month. can anybody see what i'm doing wrong?
thanks in advance!


Drop Table Month_Days;
Create Table Month_Days(
Month Number(2),
Days Number(2));


Declare
LoopM Binary_Integer;
LoopD Binary_Integer;
Begin_Date Date;
End_Date Date;
Begin
LoopM:= 0;
LoopD:= 0;

Loop
Begin_Date:= To_Date('01-Jan-2008', 'DD, Mon, YYYY');
End_Date:= Last_Day(Begin_Date);
LoopM:=LoopM+1;
LoopD:=End_Date-Begin_Date+1;
IF LoopM=13 Then
Exit;
End IF;
End_Date:= Add_Months(Begin_Date, 1);
Insert Into Month_Days Values (LoopM, LoopD);
End Loop;
End;
Reply With Quote
  #2 (permalink)  
Old 04-11-03, 08:33
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Re: Dates and Loops question

Yes: every time you go round the loop you reset Begin_Date to 01-Jan-2008, so you always set LoopD to the number of days in January.

Your code is way over-complicated, and doesn't make use of basic PL/SQL constructs like the FOR loop, e.g.

FOR LoopM IN 1..12 LOOP
...
END LOOP;

Here is a working version of your code:

Code:
Declare LoopD Binary_Integer; Begin_Date Date; End_Date Date; Begin Begin_Date:= To_Date('01-Jan-2008', 'DD, Mon, YYYY'); FOR LoopM IN 1..12 Loop End_Date:= Last_Day(Begin_Date); LoopD:=End_Date-Begin_Date+1; Insert Into Month_Days Values (LoopM, LoopD); Begin_Date:= Add_Months(Begin_Date, 1); End Loop; End; /
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On