Copy data from one table to another with different column names

I have a problem copying one data table to another. I have about 100 separate tables, which usually have the same field names, but not always. I need to be able to copy and display fields. For example: the original table is BROWARD and has the column names broward_ID, name, dob, address (the list goes on). The temp table I want to copy has an identifier, name, dob, address, etc.

I would like to match fields like broward_ID = ID, name = name, etc. But many other tables are different from the column name, so I will have to write a query for each of them. As soon as I find out the first, I can do the rest. Also, the column in both tables is out of order .. sets in advance for TSQL ...

+4
source share
2 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, follow these steps:

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 only want to copy the values โ€‹โ€‹of broward_IDand name, then:

INSERT INTO TEMP(ID, name) SELECT broward_ID,NAME FROM BROWARD
+7
source

Your question will be resolved through the update.

Consider that we have two different tables

Table A
Id Name
1  abc
2  cde

Table B
Id Name
1  
2

In the above case, you want to insert the column table of table A into the column column of table B

update B inner join on B.Id = A.Id set B.Name = A.Name where ...
0
source

Source: https://habr.com/ru/post/1568391/


All Articles