I am preparing a procedure which will extract data from several tables and output a .csv file, but have an unsurmountable problem to streamline the code. The following is an illustraion of the problem.
This code works out straightforeward:
declare
cursor cur_company is
select company_name, no_employees
from company;
line_out varchar2(200);
begin
for rec in cur_company loop
line_out := '';
line_out := line_out || rec.company_name || ',';
line_out := line_out || rec.no_employees;
dbms_output.put_line(line_out);
end loop;
end;
/
PL/SQL procedure successfully completed.
USCABLES,250
KODAK,2500
KLM,5000
HAMBRO,2000
FORD,8000
CITYBANK,3000
DISNEY,2500
In the example each record has only two fields. But the procedure I am working on deals with dozens of fields, say 60, and thus the same statement of concatenation will repeat 60 times:
for rec in cur_company loop
line_out := '';
line_out := line_out || rec.field1 || ',';
line_out := line_out || rec.field2 || ',';
...
line_out := line_out || rec.field60;
...
end loop;
The code is therefore not sightly. I thought of embedding another loop to cycle through all the fields. The following is the code, having a lot fewer lines of code than 60, but it incurred error:
declare
cursor cur_company is
select company_name, no_employees
from company;
line_out varchar2(200);
headerset varcharlist := varcharlist('COMPANY_NAME', 'NO_EMPLOYEES');
begin
for rec in cur_company loop
line_out := '';
for ctr in 1..headerset.count loop
if ctr = headerset.count then
line_out := line_out || rec.headerset(ctr);
else
line_out := line_out || rec.headerset(ctr) || ', ';
end if;
end loop;
dbms_output.put_line(line_out);
end loop;
end;
/
...
ORA-06550: line 12, column 41:
PLS-00302: component 'HEADERSET' must be declared
...
Could there be a a problem with the HEADERSET array? The following shows the array is OK:
declare
headerset varcharlist := varcharlist('COMPANY_NAME', 'NO_EMPLOYEES');
begin
for ctr in 1..headerset.count loop
dbms_output.put_line(headerset(ctr));
end loop;
end;
/
PL/SQL procedure successfully completed.
COMPANY_NAME
NO_EMPLOYEES
Or is it due to the direct use of the array for naming the fields? I tried putting the array element in a varchar2 variable before using it. But it incurred the same kind of error:
declare
cursor cur_company is
select company_name, no_employees
from company;
line_out varchar2(200);
headerset varcharlist := varcharlist('COMPANY_NAME', 'NO_EMPLOYEES');
col_name varchar2(20);
begin
for rec in cur_company loop
line_out := '';
for ctr in 1..headerset.count loop
col_name := headerset(ctr);
if ctr = headerset.count then
line_out := line_out || rec.col_name;
else
line_out := line_out || rec.col_name || ', ';
end if;
end loop;
dbms_output.put_line(line_out);
end loop;
end;
/
...
ORA-06550: line 14, column 41:
PLS-00302: component 'COL_NAME' must be declared
...
Now it seems to me that, in looping through the fields of a cursor record, only literals are accepted by PL/SQL for the field name, and variables are not recognized. Is that true? Is there a way to get around this limitation to reduce the clutter of code?