Redeclare in iterations for

I want use redeclare in sentence for;
I want export results in diferente tables;


set NLote:={i in 1…2};
param NExe{NLote};
param Lote{NLote};

let NExe[1]:=1;
let Lote[1]:=1;

table Tablax OUT “ODBC” ($outdir & “/BD.accdb”) (‘Tabla_1’ ):

for {i in 1…2}
redeclare table Tablax OUT “ODBC” ($outdir & “/BD.accdb”)
(‘Tabla_’& i ):
write table Tablax ;


Msg error:

Abandoning compound command to accept declaration of Tablax  .
   context:   >>> [NLote],NExe,Lote; <<< 

Rejecting declaration because it uses loop dummy i.
  context:   >>> [NLote],NExe,Lote; <<<
1 Like

Inside an AMPL “for” or “repeat” loop, you cannot define any model components, including tables; this applies also to re-defining them using “redeclare”. That’s the reason that you see “Abandoning compound command to accept declaration of Tablax.”

Instead, you can define an indexed collection of tables before the loop:

table Tablax {i in 1..2} OUT "ODBC" ($outdir & "/BD.accdb") ('Tabla_'&i):

for {i in 1..2}
   write table Tablax[i];

This writes the same data into both tables, however. If you just want to write all the values from NExe and Lote to the database, this should be enough:

table Tablax OUT "ODBC" ($outdir & "/BD.accdb") ('Tabla_1'):
write table Tablax;

Note also that the indexed expression should be 1..2 with two dots between the 1 and the 2.