question

Enrique Elizaga avatar image
0 Likes"
Enrique Elizaga asked martin.j answered

How to append rows of one table into another one with the same structure?

Is there a way to add rows of Table A to table B using SQL? Both have the same structure, (columns). I wouldnt want to use SELECT and add each column manually because my tables have many columns.

Below an image of my question.

FlexSim 18.0.0
tablessql queries
add.jpg (65.7 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Arun Kr avatar image
0 Likes"
Arun Kr answered Enrique Elizaga commented

Hi Enrique Elizaga,

Here's a solution using loops and SQL queries. Maybe a better solution is available and on searching through the user manual, couldn't find UNION type function to unite two tables. Not sure about, whether this can be done using INNER JOIN. This solution is something, which quickly came to my mind.

On a higher level, what I have did here is stored the data from two tables into an array and updated the second table with the data stored in the array.

Table TableA = Table("TableA");
Table TableB = Table("TableB");
int ArraySize = TableA.numRows + TableB.numRows;
Array TableDataRows = Array(ArraySize);
Array TableDataCols = Array(TableA.numCols);
Table resultA = Table.query("SELECT *FROM TableA");// Selecting From TableA
int NrofTableARows = TableA.numRows;
int NrofTableACols = TableA.numCols;
for(int i=1;i<=NrofTableARows;i++) // Storing Result in the Array
{
	for(int k=1;k<=NrofTableACols;k++)
	{
	 TableDataCols[k]=	resultA[i][k];
	}
	TableDataRows[i] = TableDataCols.clone();
}
Table resultB = Table.query("SELECT *FROM TableB");// Selecting From TableB
int NrofTableBRows = TableB.numRows;
int NrofTableBCols = TableB.numCols;
for(int i=1;i<=NrofTableBRows;i++)// Storing Result in the Array
{
	for(int k=1;k<=NrofTableBCols;k++)
	{
	 TableDataCols[k]=	resultB[i][k];
	}
	TableDataRows[i+ NrofTableBRows] = TableDataCols.clone();
}
TableB.setSize(ArraySize,3);//Setting the table size.
for(int i=1;i<=ArraySize;i++) // Writing Data to the Table from Array
{
	for(int k=1;k<=NrofTableBCols;k++)
	{
		TableB[i][k]= TableDataRows[i][k];
	}
}

See the attached model.

Regards,

Arun KR

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Enrique Elizaga avatar image Enrique Elizaga commented ·

Wow @Arun KR that seems complicated. Wouldnt it be easier:

for(int ROW=1; ROW <= Table("Origin").numRows; ROW++)
{
	Table("Destination").addRow();
	for(int COL=1; COL<= Table(x).numCols; COL++)
	{
		Table("Destination")[Table("Destination").numCols][COL] =
			Table("Origin")[ROW][COL];
	}	
}
0 Likes 0 ·
martin.j avatar image
4 Likes"
martin.j answered

Update for future reference: Apparently the UNION ALL SQL feature is now available in FlexSim SQL interface, so this task is possible using one of the new calculated tables. It must have been added in Flexsim 19.01 or 20.01

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.