Redeclare in iterations for

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

thanks

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’ ):
[NLote],NExe,Lote;

for {i in 1…2}
{
redeclare table Tablax OUT “ODBC” ($outdir & “/BD.accdb”)
(‘Tabla_’& i ):
[NLote],NExe,Lote;
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):
   [NLote],NExe,Lote;

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'):
   [NLote],NExe,Lote;
write table Tablax;

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