Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PLS 00302 component must be declared

Something happens that I dont understand.

here is a type declared:

type my_type is record (
    amount number(18,2),
    my_date date
)

my_variable my_type 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for my_variable in (select sum(table_amount), table_date from table group by table_date)
loop
   dbms_output.put_line(my_variable.my_date);
   dbms_output.put_line(my_variable.amount)
end loop;

my_variable.my_date works good but my_variable.amount triggers a PLS00302 error that I dont understand.

table structure:
table_date date,
table_amount number(10,2)

>Solution :

The loop declares a local variable my_variable that shadows the previous declaration. You need to reference the column names and not the record’s attributes:

DECLARE
  type my_type is record (
    amount number(18,2),
    my_date date
  );

  my_variable my_type;
BEGIN
  for my_variable in (select table_amount, table_date from table_name)
  loop
    dbms_output.put_line(my_variable.table_date);
    dbms_output.put_line(my_variable.table_amount);
  end loop;
END;
/

Which is the same as:

DECLARE
  type my_type is record (
    amount number(18,2),
    my_date date
  );

  my_variable my_type;
BEGIN
  for not_my_variable in (select table_amount, table_date from table_name)
  loop
    dbms_output.put_line(not_my_variable.table_date);
    dbms_output.put_line(not_my_variable.table_amount);
  end loop;
END;
/

(But is slightly clearer as you do not have the cursor loop variable shadowing the declared variable.)

db<>fiddle here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading