0

I am trying to run this code

declare 
temp_atr_val varchar2(400);
temp_val varchar2 (400);
temp_sum_percent decimal (10,3);
temp_variable number (10,3);
column_count number ; 
val_count number;
sales_store number;
begin    
select count(distinct ATTRIBUTE_INPUT) into column_count from look_up_table;
for ind in 1..column_count loop

    /* putting current value of attribute from look_up_table in temp variable*/
    select ATTRIBUTE_INPUT into temp_atr_val from (
        select ATTRIBUTE_INPUT, rownum rwn
        from 
        (
           select distinct ATTRIBUTE_INPUT
           from look_up_table
        )
    ) where rwn = ind;

    select count( value_for_atr ) into val_count from look_up_table;

    for ind in 1..val_count loop

   /* putting current value_for_atr for corresponding attribute from look_up_table in temp variable*/
        select value_for_atr into temp_val from 
         (
          select value_for_atr, rownum rwn
          from look_up_table
         ) where rwn = ind;

       SELECT SUM(CASE WHEN temp_atr_val = temp_val THEN net_sales_home ELSE 0 END) into temp_variable
       FROM schemafinal;

 /*temp_variable := temp_variable/sales_store;*/


 EXECUTE IMMEDIATE 'ALTER TABLE SAR ADD (percent_'||temp_val||' number)';
 EXECUTE IMMEDIATE ' update SAR b 
 set b.percent_'||temp_atr_val||'_'||temp_val||' = 105 ';


END LOOP;
END LOOP;   
END;

But every time the code shows one of the 2 errors: ORA-01430: column being added already exists in table

  1. 00000 - "missing right parenthesis"

And when I check the SAR table only 5 new columns are made in it. I don't know why this is happening, have tried almost everything.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140

1 Answers1

0

It is difficult to analyze the query without knowing the data structure.

But I am assuming you need an extra ATTRIBUTE_INPUT condition in the second loop select query to find temp_val. Otherwise for each ATTRIBUTE_INPUT temp_val will return same result.

That means in the second iteration of first loop temp_val will be the same old values of first iteration. So the Alter table Add query will throw error.

So please try to add an additional ATTRIBUTE_INPUT condition

   /* putting current value_for_atr for corresponding attribute from look_up_table in temp variable*/
        select value_for_atr into temp_val from 
         (
          select value_for_atr, rownum rwn
          from look_up_table
          where ATTRIBUTE_INPUT = temp_atr_val
         ) where rwn = ind;
Pradeeshnarayan
  • 1,235
  • 10
  • 21
  • I tried and its not showing any error. but now the new columns are not added in the SAR table. I dont know if the temporary variable is even taking proper value. how do i check that? – user1617511 Aug 30 '12 at 10:15