SQL 将数据从一张表插入到另一张表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6846485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:29:51  来源:igfitidea点击:

insert data from one table to another

sql

提问by lisa

I have 2 different tables but the columns are named slightly differently. I want to take information from 1 table and put it into the other table. I need the info from table 1 put into table 2 only when the "info field" in table 1 is not null. Table 2 has a unique id anytime something is created, so anything inserted needs to get the next available id number.

我有 2 个不同的表,但列的名称略有不同。我想从 1 个表中获取信息并将其放入另一个表中。仅当表 1 中的“信息字段”不为空时,我才需要将表 1 中的信息放入表 2。表 2 在任何时候创建的东西都有一个唯一的 id,所以任何插入的东西都需要获得下一个可用的 id 号。

Table 1

表格1

category
clientLastName
clientFirstName
incidentDescription
info field is not null then insert all fields into table 2

Table 2

表 2

*need a unique id assigned
client_last_name
client_first_name
taskDescription
category

回答by Narnian

This should work. You don't need to worry about the identify field in Table2.

这应该有效。您无需担心 Table2 中的识别字段。

INSERT INTO Table2
 (client_last_name, client_first_name, taskDescription, category)
 (SELECT clientLastName, clientFirstName, incidentDescription, category
  FROM Table1
  WHERE info_field IS NOT NULL)

回答by Ashan

Member_ID nvarchar(255) primary key,
Name nvarchar(255),
Address nvarchar(255)
)
insert into Member(Member_ID,Name,Address) (select m.Member_Id,m.Name,m.Address from library_Member m WHERE Member_Id IS NOT NULL)