Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 8th, 2012, 05:06 PM
AndroidZ AndroidZ is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 AndroidZ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 49 sec
Reputation Power: 0
PL/SQL loop types and their behavior

Code:
set serveroutput on;

DECLARE
        num number(1) :=1;
        num2 number(1) := 1;
BEGIN
        WHILE num < 5 LOOP
                WHILE num2 < 5 LOOP
                        DBMS_OUTPUT.PUT_LINE( num || '+' || num2 || '=' || (num + num2) );
                        num2 := num2 + 1;
                END LOOP;
                num := num + 1;
        END LOOP;
END;
/

the output for this loop is:
1+1=2
2+2=4
3+3=6
4+4=8

this is what I wanted. Both variables are incrementing together.

However, when I try to use a FOR loop to do the same thing:

Code:
set serveroutput on;

DECLARE
        num number(1) :=1;
        num2 number(1) := 1;
BEGIN
        FOR i in 1..4 LOOP
                FOR i in 1..4 LOOP
                        DBMS_OUTPUT.PUT_LINE( num || '+' || num2 || '=' || (num + num2) );
                        num2 := num2 + 1;
                END LOOP;
                num := num + 1;
        END LOOP;
END;
/


I get the result:

1+1=2
1+2=3
1+3=4
1+4=5
2+5=7
2+6=8
2+7=9
2+8=10
3+9=12
DECLARE
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: number precision too large
ORA-06512: at line 8


I understand the output means the inner loop is running its course before the outer loop is started and then continuing on to exceed its limit, but I don't understand why this is or how to make it so I get the same results as the WHILE loops. Any help is appreciated.

Reply With Quote
  #2  
Old November 9th, 2012, 02:23 PM
LKBrwn_DBA's Avatar
LKBrwn_DBA LKBrwn_DBA is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 751 LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 2 h 56 m 5 sec
Reputation Power: 348
Cool

Quote:
Originally Posted by AndroidZ
... Etc ..., but I don't understand why this is or how to make it so I get the same results as the WHILE loops.


If you want both variables to increment together, you only need one FOR..LOOP.
__________________

Reply With Quote
  #3  
Old November 9th, 2012, 11:17 PM
AndroidZ AndroidZ is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 AndroidZ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 49 sec
Reputation Power: 0
I should mention, the problem I'm working on requires two for loops, one for each variable.

EDIT: After adding 'outer' and 'inner' to the output to track the loops, the pattern is 1 outer, 4 inner, so the inner loop is running all the way through for each iteration of the outer loop, but I still haven't been able to remedy this.

Reply With Quote
  #4  
Old November 10th, 2012, 12:25 PM
AndroidZ AndroidZ is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 AndroidZ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 49 sec
Reputation Power: 0
I feel like such a fool... All I needed to do was change the inner loop to
Code:
FOR i in 1..1
.

Reply With Quote
  #5  
Old November 13th, 2012, 10:16 AM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
Quote:
Originally Posted by AndroidZ
I feel like such a fool... All I needed to do was change the inner loop to
Code:
FOR i in 1..1
.

Which negates the need for a for loop as previously pointed out.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > PL/SQL loop types and their behavior

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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 On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap