0

At this handler I delete NULL element:

if p_arr(i) is null then
    p_arr.delete(p_arr(i));
end if;

but null element still in collection. How can I correct this? The task is to sort elements in collection, but the condition is that there can be NULL in collection. I should delete them.

declare   
p_arr dbms_sql.Number_Table;   
i pls_integer;  

procedure do_sort(p_arr in out dbms_sql.Number_Table, p_asc in boolean 
default null, p_nulls_last in boolean default null) is   
x pls_integer;  
p_temp number;

begin  

for i in -1..p_arr.COUNT - 2 

loop   
  if p_arr(i) is null then
    p_arr.delete(p_arr(i));
  end if;        

end loop;  

end; 

begin   
p_arr(-1) := 0;   
p_arr(0) := -2;   
p_arr(1) := 10.1;   
p_arr(2) := null;    
p_arr(3) := 10.1;   
p_arr(4) := -1;  

do_sort(p_arr); 

i := p_arr.first;   

while i is not null loop   
  dbms_output.put_line('arr('||i||') = '||nvl(to_char(p_arr(i)), 
'null')||';');   
i := p_arr.next(i);   
end loop;   

end; 

1 Answer 1

2

That's because you are passing the element itself as the argument to the delete, rather than the index

p_arr.delete(p_arr(i)) is equivalent to p_arr.delete(NULL) - So it simply doesn't delete anything.

So, change it to p_arr.DELETE(i) it will work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.