0

I'm trying to add some ids into an array:

CREATE OR REPLACE TYPE array_of_numbers AS VARRAY(10)OF NUMBER(10);

declare
    student_ids array_of_numbers;
begin
    select nrleg BULK COLLECT into student_ids from student where nrleg = 123458;
    FOR i IN 1..student_ids.COUNT LOOP
        DBMS_OUTPUT.PUT_LINE(student_ids(i));
    END LOOP;
end;

but I get the following errors:

--------- -------------------------------------------------------------
3/1       PLS-00103: Encountered the symbol "DECLARE" 
10/4      PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:     ( begin case declare end exception exit for goto if loop mod    null pragma raise return select update while with    <an identifier> <a double-quoted delimited-identifier>    <a bind variable> << continue close current delete fetch lock    insert open rollback savepoint set sql execute commit forall    merge pipe purge json_exists json_value json_query    json_object json_array 
Errors: check compiler log

Could someone explain what I did wrong?

4
  • Remove DECLARE; it is not to be used in named PL/SQL procedures. Commented Jun 3, 2021 at 14:45
  • @Littlefoot but if I remove the declare part I won't have an array to save the data into (?) Commented Jun 3, 2021 at 14:49
  • I didn't say to remove the whole section, but the DECLARE keyword. Commented Jun 3, 2021 at 14:50
  • 1
    Ah, I'm sorry, blinded by how-knows-what. This is an anonymous PL/SQL block. The only thing that's missing is a slash that terminated the CREATE TYPE statement. Sorry. Commented Jun 3, 2021 at 14:55

1 Answer 1

2

Some DDL needs to be terminated with a /; a type can have a PL/SQL body so this is one of them. To run that all at once as a script do:

CREATE OR REPLACE TYPE array_of_numbers AS VARRAY(10)OF NUMBER(10);
/

declare
 student_ids array_of_numbers;
begin
select nrleg BULK COLLECT into student_ids from student where nrleg = 123458;
    FOR i IN 1..student_ids.COUNT LOOP
        DBMS_OUTPUT.PUT_LINE(student_ids(i));
    END LOOP;
end;
/

You don't need the semicolon on the end of the CREATE here, but it doesn't hurt; but for some other commands (including other DDL like create table) having both would cause it to try to execute the statement twice, which could cause an error.

SQL Developer won't complain about the lack of a / after the last PL/SQL block in a script, but other tools will, so it's better to always include that one too.

db<>fiddle


Incidentally, another way to see the contents of the array in SQL Developer is with a ref cursor:

var rc refcursor

declare
  student_ids array_of_numbers;
begin
  select nrleg BULK COLLECT into student_ids from student where nrleg = 123458;
  open :rc for select * from table(student_ids);
end;
/

print rc

... but then you might as well just select directly from the table, without any PL/SQL.

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

5 Comments

Now I'm trying to print the numbers from the array like this: for i in student_ids.first..student_ids.last loop DBMS_OUTPUT.PUT_LINE(student_ids(i)); end loop; but I get this :end loop Error report - Unknown Command
@user14769698 - that error is from the client; so are you trying to do that outside a PL/SQL block? it works inside one.
I added BEGIN and END; but it I get the same thing.
It's unclear what you're doing (or doing wrong); the fiddle shows that it works if you run it properly.
Aaah thank you, it worked with the cursor!

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.