【问题标题】:insert data from one table to another将数据从一个表插入到另一个表
【发布时间】:2013-07-29 16:43:32
【问题描述】:

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

表 1

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

表 2

*need a unique id assigned
client_last_name
client_first_name
taskDescription
category

【问题讨论】:

标签: sql


【解决方案1】:

这应该可以。您无需担心 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)

【讨论】:

  • 名称的不同无关紧要。
【解决方案2】:
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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2014-10-15
    • 1970-01-01
    • 2020-09-28
    • 2017-03-05
    • 2013-04-25
    相关资源
    最近更新 更多