views:

159

answers:

1

I've got a PL/SQL VArray that I'm filling with a BULK COLLECT query like this:

SELECT id
BULK COLLECT INTO myarray
FROM aTable

Now I'd like to pass a slice of this collection into another collection, something like this:

newarray := myarray(2..5)

This should pass the elements 2,3,4 and 5 from myarray to newarray.

I could write a loop and copy the elements, but is there a more compact way to do this?

+3  A: 

Generally, you don't want to do this. You have a large collection in memory, and now you want to make a copy of it. That would use even more memory. Usually in cases like this you pass the entire collection around (by reference, not value) and also provide a start and stop index. Leave it to the other functions to only process the range specified.

Adam Hawkes
Well, for my specific use case I don't really see a problem as I'll be mostly dealing with pretty small collections (<10 entries - I omitted the where criteria in my query).The small memory/runtime overhead of doing the copy seems to me smaller than the code complexity it would introduce.
IronGoofy
+1, PL/SQL isn't C - no pointers.
DCookie
Not pointers exactly, but you can at a `NOCOPY` hint on a parameter. See http://www.dba-oracle.com/t_plsql_passing_data_structures_nocopy.htm
Adam Hawkes