简体   繁体   English

插入SQL Server 2005

[英]Insert into SQL Server 2005

I have a simple problem. 我有一个简单的问题。 I have two tables FROM and TO . 我有两个表FROMTO I want to copy column Group from FROM to TO but only the rows which match the id_number . 我想将列GroupFROM复制到TO但只复制与id_number匹配的id_number It should be very simple but I am stuck. 它应该很简单,但我卡住了。

insert into TO (Group)
   SELECT Group 
   FROM FROM
   WHERE FROM.id_number like TO.id_number

OR 要么

INSERT INTO TO (Group)
   SELECT Group 
   FROM FROM
   WHERE id_number IN (SELECT id_number FROM TO)

This one just adds additional columns to the table, it does not update the rows as per id_number .... 这个只是在表中添加了额外的列,它不会根据id_number更新行....

Any idea? 任何想法?

You say "only the rows which match the id_number". 你说“只有与id_number匹配的行”。 That implies update not insert . 这意味着updateinsert

update [TO] set [Group] = [FROM].[Group]
from [FROM]
inner join [TO] on [FROM].id_number = [TO].id_number

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

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