Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying data from one table to another different column names

Tags:

sql

database

tsql

I'm having an issue copying one table's data to another. I have around 100 or so individual tables that have generally the same field names but not always. I need to be able to copy and map the fields. example: source table is BROWARD and has column names broward_ID, name, dob, address (the list goes on). The temp table I want to copy it to has ID, name, dob, address etc.

I'd like to map the fields like broward_ID = ID, name = name, etc. But many of the other tables are different in column name, so I will have to write a query for each one. Once I figure out the first on, I can do the rest. Also the column in both tables are not in order either..thanks in advance for the TSQL...

like image 902
Sal Avatar asked Dec 19 '14 20:12

Sal


1 Answers

With tables:

BROWARD (broward_ID, name, dob, address) /*source*/
TEMP (ID, name, address,dob) /*target*/

If you want to copy information from BROWARD to TEMP then:

INSERT INTO TEMP SELECT broward_ID,NAME,ADDRESS,DOB FROM BROWARD --check that the order of columns in select represents the order in the target table

If you want only copy values of broward_ID and name then:

INSERT INTO TEMP(ID, name) SELECT broward_ID,NAME FROM BROWARD
like image 105
Aramillo Avatar answered Oct 05 '22 23:10

Aramillo