I am trying to insert columns in a table using a for loop that iterates over a cursor. The code is:
declare
cursor Months_ET is
SELECT distinct to_char(FEE_CD_ACT_SUM_ACCTG_DA, 'MON-YY') as "Month_U"
FROM EXPORT_TABLE
WHERE EXPORT_TABLE.FEE_CD_ACT_SUM_ACCTG_DA >= to_date('10/01/2013','mm/dd/yyyy')
AND EXPORT_TABLE.FEE_CD_ACT_SUM_ACCTG_DA < to_date('10/01/2014', 'mm/dd/yyyy');
n integer := 1;
begin
for mon in Months_ET loop
dbms_output.put_line(mon."Month_U");
execute immediate 'Alter table "Fee_CT" add('|| mon."Month_U" ||' varchar(20))';
n := n +1;
end loop;
end;
The cursor in the beginning jsut gets a list of unique month names which the dbms_output.put_line prints out as:
SEP-14
AUG-14
JUL-14
So I know the variable is not empty.
So using those results I want to add three columns for each month- yr. However I get an invalid datatype error. I have also tried altering to the for loop to concatenate the table name outside of the quotes like this:
for mon in Months_ET loop
--Month_List(n) := mon."Month_U";
dbms_output.put_line(mon."Month_U");
execute immediate 'Alter table' ||"Fee_CT" || 'add('|| mon."Month_U" ||' varchar(20))';
n := n +1;
But I get a message that "Table,View Or Sequence reference 'Fee_CT' not allowed in this context." Not sure what I am doing wrong. The actual data is much larger and covers a wider time frame so using multiple alter table statements isn't realistic. plus the underlying data will be changing, so I need to be able to change the column names with the underlying data.