【问题标题】:Unable to copy data from one table to another where id=1无法将数据从一个表复制到另一个 id=1 的表
【发布时间】:2013-05-30 06:05:56
【问题描述】:

嘿,伙计们,我是这里的新手,我想做的是我正在尝试 将数据(技能)列从一个临时表复制到另一个 id=1 的表;

现在名为 Skillmapping 的表有 CandidateId 和 SkillName 列

Table2 只有 SkillName 列

我正在尝试将数据从表 2 插入到 canidateId=1 的表 1

请帮我解决这个问题。我尝试复制数据,但对于候选 ID 来说似乎是错误的 .. 谢谢。

我试过了,但说语法不正确

 insert into candidateComputerSkillMapping(skillname) values(select * from tempComputerSkillMap)

感谢所有的答案伙计们,我猜两个表上的列应该是相同的,我的意思是两者都应该有一个 id 和 SkillName 然后只有我们可以复制权利.. 无论如何,谢谢,

【问题讨论】:

  • insert into Table2 select skillname from Table1 where candidateid = 1
  • 您的插入查询错误而不是select * from tempComputerSkillMap 尝试使用select skillname from tempComputerSkillMap,因为您只想将技能名称值插入表candidateComputerSkillMapping..希望您理解,* 表示all columns ..
  • insert into candidateComputerSkillMapping select skillName from tempComputerSkillMap where candidateid = 1 应该可以工作。请发布您的创建表脚本
  • 这并不重要both tables should be same i mean both should have an id and skillName then only we can copy right..实际上如果你的id是主键那么它会产生问题因为你不能将null值分配给主键而inserting否则那没有没关系。

标签: sql tsql


【解决方案1】:

尝试进行类似的查询

INSERT INTO candidateComputerSkillMapping (skillName)
SELECT skillName
FROM tempComputerSkillMap
WHERE candidateId=1;

或者

INSERT INTO candidateComputerSkillMapping
SELECT skillName
FROM tempComputerSkillMap
WHERE candidateId=1;

查看sqlauthority

如果candidateComputerSkillMapping 表中的candidateIdprimaryid,那么你不能将null 插入主键,它会报错。这就是为什么显示这两个表结构以获得好的答案。

希望它有效。

【讨论】:

  • 亲爱的你想插入table 2数据到table 1table 1数据到table 2..
  • 我正在尝试将数据从 table2 插入到 table1,问题是 table2 只包含技能名,我想将该数据添加到我们有技能 ID=1 的表 1
【解决方案2】:

如果您认为数据已经存在并且需要更新它,那么...

update Table2 set skillname = (select skillname from Table1 where candidateid =1)

如果您认为需要插入新记录,那么......

insert into Table2 select skillname from Table1 where candidateid = 1

【讨论】:

  • 列名或提供的值的数量与表定义不匹配。这是错误。
  • 尝试...插入 Table2 (skillname) 值(从 Table1 中选择技能名称,其中候选 ID=1)
  • @PavanKumarK 您的查询应该可以工作insert into Table2 select skillname from Table1 where candidateid = 1
  • 是的 Pavan 的查询错误,实际上,我也试过了,但它说错误的语法插入到 CandidateComputerSkillMapping(skillname) 值中(从 tempComputerSkillMap 中选择 *)
  • @designerNProgrammer 你能发布表格脚本吗?
【解决方案3】:
INSERT INTO [Table2] SELECT skillName FROM [skillmapping] WHERE candidateId = 1

【讨论】:

  • 它说错误列名称或提供的值的数量与表定义不匹配。我想是因为我在技能映射表上没有候选人 ID.. 还有其他方法吗?谢谢
  • 正如你所说,你的技能映射表上有candidateId。我理解错了吗?
【解决方案4】:

由于您的table2 没有candidateid,我假设在插入时,您确定table2 中包含的所有技能都属于candidateid = 1。

如果假设正确,那么您可以按照下面的示例将table2的新技能插入table1

编辑:使用 cmets 更新查询。

use tempdb

-- Pretending this is your table1
create table candidateComputerSkillMapping
(
    candidateid int             not null
    , skillname varchar(128)    not null
    , unique 
    (
        candidateid
        , skillname
    )
)

-- Table1 has one skill for candidateid = 1
insert candidateComputerSkillMapping values (1, 'Old Skill')

-- Prentending this is your table2
create table tempComputerSkillMap
(
    skillname varchar(128) not null
)

-- table2 has two skills. Now since this table doesn't have candidateid, how do I know the skills are for candidateid = 1?
-- I don't know and I am assuming that you are certain that these skills indeed belong to candidateid = 1.
insert tempComputerSkillMap values ('Old skill')
insert tempComputerSkillMap values ('New skill')

-- Insert only new skills from table2 into table1 for candidateid = 1
insert candidateComputerSkillMapping (candidateid, skillname)
select 1, t2.skillname
from tempComputerSkillMap t2
where not exists (select * from candidateComputerSkillMapping t1 where t1.candidateid = 1 and t1.skillname = t2.skillname)

select * from candidateComputerSkillMapping

【讨论】:

  • 这很混乱,请你详细告诉我。非常感谢
  • 如果您想要更有针对性的回复,请使用表格定义和示例数据更新您的问题。
猜你喜欢
  • 1970-01-01
  • 2011-05-10
  • 1970-01-01
  • 2018-07-10
  • 2012-11-24
  • 2011-05-16
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多