简体   繁体   English

将数据从一个表复制到另一个不同的列名

[英]Copying data from one table to another different column names

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. 我有大约100个左右的单个表,这些表通常具有相同的字段名称,但并非总是如此。 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). 示例:源表是BROWARD并且具有列名broward_ID,name,dob,address(列表继续)。 The temp table I want to copy it to has ID, name, dob, address etc. 我要复制它的临时表有ID,名称,dob,地址等。

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. 我想映射像broward_ID = ID,name = name等字段。但是许多其他表的列名不同,所以我将不得不为每一个写一个查询。 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... 此外,两个表中的列都没有按顺序排列。提前感谢TSQL ...

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: 如果要将信息从BROWARD复制到TEMP,则:

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: 如果您只想复制broward_IDname值,那么:

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

Your question will resolve using update 您的问题将使用更新解决

Let's consider we have two different table 让我们考虑一下我们有两个不同的表

Table A
Id Name
1  abc
2  cde

Table B
Id Name
1  
2

In above case want to insert Table A Name column data into Table B Name column 在上面的情况下,要将Table A Name列数据插入Table B Name列

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM