Home » SQL & PL/SQL » SQL & PL/SQL » Loop and save the index in an incremental number array
Loop and save the index in an incremental number array [message #686941] Mon, 06 February 2023 09:03 Go to next message
Joy83
Messages: 6
Registered: February 2023
Junior Member
Hello
I want to build an array of numbers ghat hold 31 value
From 1..2..3……….31

And I want to create a loop for counter in 1 to 31
And inside the loop I will do some calculations
Then save the value in the first array.
Something like
Var(counter)

When I put it this way I get error.
What is the best way to do it and save 31 values based on the position of the loop

Basically if the counter =1
Var(1)= my value here

If the counter =2
Var(2)= my value here
And so on
Re: Loop and save the index in an incremental number array [message #686942 is a reply to message #686941] Mon, 06 February 2023 09:32 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Welcome to the forum.
Please read the OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Indent the code, use code tags and align the columns in result.

Also always post your Oracle version, with 4 decimals (query v$version), as often solution depends on it.


Quote:
When I put it this way I get error.

What error?
Use SQL*Plus and copy and paste what you already tried.

A good reference: PL/SQL User's Guide and Reference.

Re: Loop and save the index in an incremental number array [message #686943 is a reply to message #686942] Mon, 06 February 2023 10:48 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
...more precisely:

[Updated on: Mon, 06 February 2023 10:49]

Report message to a moderator

Re: Loop and save the index in an incremental number array [message #686944 is a reply to message #686943] Mon, 06 February 2023 11:51 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
SCOTT@orcl_12.1.0.2.0> DECLARE
  2    TYPE my_varray IS VARRAY(31) OF NUMBER;
  3    var	 my_varray := my_varray();
  4    my_value  NUMBER := 0;
  5  BEGIN
  6    var.extend(31);
  7    FOR counter IN 1 .. 31 LOOP
  8  	 my_value := my_value + 2;
  9  	 var(counter) := my_value;
 10    END LOOP;
 11    FOR counter IN 1 ..31 LOOP
 12  	 DBMS_OUTPUT.PUT_LINE (counter || '   ' || var(counter));
 13    END LOOP;
 14  END;
 15  /
1   2
2   4
3   6
4   8
5   10
6   12
7   14
8   16
9   18
10   20
11   22
12   24
13   26
14   28
15   30
16   32
17   34
18   36
19   38
20   40
21   42
22   44
23   46
24   48
25   50
26   52
27   54
28   56
29   58
30   60
31   62

PL/SQL procedure successfully completed.
Re: Loop and save the index in an incremental number array [message #686945 is a reply to message #686944] Mon, 06 February 2023 12:40 Go to previous messageGo to next message
Joy83
Messages: 6
Registered: February 2023
Junior Member
Thanks a lot
This is helpful

Can I sum var(1)+var(2)…var(31)
In a simpler and shorter way rather than repeating them 31.
Like sum a range or something
Because I have to do this multiple times
Re: Loop and save the index in an incremental number array [message #686946 is a reply to message #686944] Mon, 06 February 2023 13:25 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

@Barbara

Sad

Tell me and I'll forget; show me and I may remember; involve me and I'll understand

[Updated on: Mon, 06 February 2023 13:25]

Report message to a moderator

Re: Loop and save the index in an incremental number array [message #686947 is a reply to message #686945] Mon, 06 February 2023 14:37 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
Joy83 wrote on Mon, 06 February 2023 10:40
Thanks a lot
This is helpful

Can I sum var(1)+var(2)…var(31)
In a simpler and shorter way rather than repeating them 31.
Like sum a range or something
Because I have to do this multiple times
I don't understand exactly what you are asking. Please try to provide a better explanation and exact calculation and results that you want. Please show what you tried to modify what I provided and what error message or results that you got. In the example that I provided, there are no initial values in the array var; The values are calculated in the variable my_value, which is then used to populate the array. I also need to know whether you need the method you described for some reason or just the results. Chances are you can get whatever you need just using a SQL select statement without any PL/SQL or looping. The more information that you can provide and the more that you try yourself, the more that we can guide you.
Re: Loop and save the index in an incremental number array [message #686948 is a reply to message #686945] Mon, 06 February 2023 19:41 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Joy83 wrote on Mon, 06 February 2023 13:40
Thanks a lot
This is helpful

Can I sum var(1)+var(2)…var(31)
In a simpler and shorter way rather than repeating them 31.
Like sum a range or something
Because I have to do this multiple times
RTFM 4.2.6 FOR LOOP Statement

SY.
Re: Loop and save the index in an incremental number array [message #686949 is a reply to message #686948] Mon, 06 February 2023 21:15 Go to previous message
Joy83
Messages: 6
Registered: February 2023
Junior Member
Thank you very much
Previous Topic: Duplicate rows by generating date based on column value
Next Topic: XML parse
Goto Forum:
  


Current Time: Wed Apr 24 15:24:44 CDT 2024