Suggestions in this reply consider that a new table should be created and that the source tables are declared with columns in the same sequence.
(1) It is possible to insert one table at a time without the need for UNION ALL:
-- código #1
SELECT coluna_1, coluna_2, ..., coluna_n
into DATATRAN_CSV
from DATATRAN2013_CSV;
INSERT into DATATRAN_CSV (coluna_1, coluna_2, ..., coluna_n)
SELECT coluna_1, coluna_2, ..., coluna_n
from DATATRAN2014_CSV;
INSERT into DATATRAN_CSV (coluna_1, coluna_2, ..., coluna_n)
SELECT coluna_1, coluna_2, ..., coluna_n
from DATATRAN2015_CSV;
INSERT into DATATRAN_CSV (coluna_1, coluna_2, ..., coluna_n)
SELECT coluna_1, coluna_2, ..., coluna_n
from DATATRAN2016_CSV;
This way less memory resources are used.
(2) If you want to merge the source tables, before creating the new table, here is another option:
-- código #2
with cteDATATRAN as (
SELECT * from DATATRAN2013_CSV
union all
SELECT * from DATATRAN2014_CSV
union all
SELECT * from DATATRAN2015_CSV
union all
SELECT * from DATATRAN2016_CSV
)
SELECT *
into DATATRAN_CSV
from cteDATATRAN;
Please note that you must use UNION ALL to achieve full union. If you use only UNION, in addition to the process slowing down, repeat lines will be discarded.
(3) To have absolute control, I suggest that you do not use * in the column list of the source tables but set the column names:
-- código #3
with cteDATATRAN as (
SELECT coluna_1, coluna_2, ..., coluna_n
from DATATRAN2013_CSV
union all
SELECT coluna_1, coluna_2, ..., coluna_n
from DATATRAN2014_CSV
union all
SELECT coluna_1, coluna_2, ..., coluna_n
from DATATRAN2015_CSV
union all
SELECT coluna_1, coluna_2, ..., coluna_n
from DATATRAN2016_CSV
)
SELECT *
into DATATRAN_CSV
from cteDATATRAN;
This is an Insert with sub query
– Renato Junior