user10969476
user10969476

Reputation: 11

How can I append number of datasets together in SQL commander?

I have 50 datasets. Each dataset has different records of 3 variables each. I want to compile one dataset that all records from 50 datasets for those 3 variables.

Example is Table 1:

var1 var2 var3
val1 val2 val3

Table 2:

var1 var2 var3
val2 val3 val4

Table 3:

var1 var2 var3
val5 val1 val4

I have 50 such tables and I want end result as:

var1 var2 var3
val1 val2 val3
val2 val3 val4
val5 val1 val4

I assigned table 1 as my final table and tried inserting into other tables.

insert into final_table
select var1,var2,var3 from table2

Is there a way for me to repeat this operation without having to copy and paste this 50+ times?

Upvotes: -1

Views: 47

Answers (1)

Guillaume Outters
Guillaume Outters

Reputation: 1609

Some artisanal solutions, from less to more automatized;
depending on the frequency you need to execute it, maybe a fully automatized solution is not worth it.

With a spreadsheet

In a spreadsheet where you paste the table names into column A, put the following formula in B1 then extend formula to the bottom:

="insert into final_table select var1, var2, var3 from "&A1&";"

Then copy-paste column B to DBVisualizer and run them.

N.B.: you can do this with any scripting language you're at ease with.

With copy-paste in DBVisualizer

Same as with a spreadsheet, except the formula is done in SQL.
But see my comment, we need to know your DB engine and table structure to be able to answer.

In 1 consolidated instruction in the DB

See my comment too, we have to know the DB server and table structure.

Upvotes: 0

Related Questions