简体   繁体   中英

Insert into SQL Server 2005

I have a simple problem. I have two tables FROM and TO . I want to copy column Group from FROM to TO but only the rows which match the 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 ....

Any idea?

You say "only the rows which match the id_number". That implies update not insert .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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