Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ADD 5  TO COUNTER IN FOR LOOP

Posted on 2008-10-01
4
Medium Priority
?
1,148 Views
Last Modified: 2013-12-19
How can I increment  the count in a variable in the loop instead of codeing the actual numbers for the count such as (4,9,14,19,24).  In other words, I just want to add 5 to the counter beginning with record number 4 in the loop.  There can be up to 24  records in the loop.    

This is an oracle table.  I'm using PL/SQL.
See attached code.  



v_count := 0
 
for c1 rec in c1  loop
 v_count := v_count + 1;
 
if v_count in (4,9,14,19,24)  then 
  put_line (l_file,'TEST1');
end if;
 if v_count  in (5,10,15,20) then
  put_line(l_file,TEST2);
end if;
if v_count in (6,11,16,21)
 put_line(l_file,TEST2);
end if;
 
end loop;

Open in new window

0
Comment
Question by:ratliffjm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 22620019
use the "mod" function to do your "loop" check and since you only care about values 4 or higher, then simply wrap all of the conditions in that.
v_count := 0
 
for c1 rec in c1  loop
 v_count := v_count + 1;
 
  if v_count >= 4 then
    if mod(v_count,5) = 4 then  
       put_line (l_file,'TEST1');
    end if;
    if mod(v_count,5) = 0 then
       put_line(l_file,TEST2);
    end if;
    if mod(v_count,5) = 1 
       put_line(l_file,TEST2);
    end if;
  end if;
end loop;

Open in new window

0
 

Author Comment

by:ratliffjm
ID: 22620061
Thanks.   I will test the code when I'm at the office tomorrow morning.
0
 

Author Closing Comment

by:ratliffjm
ID: 31502205
Thanks
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 22636245
glad I could help
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question