简体   繁体   中英

Merge SQL table data to an existing SQL table with the same column names

I have two tables one with all the parts numbers (ITEMNO) and another with COMMENT4.

I have another table with the same names but it only has a few ITEMNO from the first list.

If I run this query I get

insert into dbo.icitem (comment4)
select COMMENT4 
from dbo.f1$ 
where dbo.icitem.ITEMNO = dbo.f1$.ITEMNO)

I get this error

Msg 4104, Level 16, State 1, Line 4

The multi-part identifier "dbo.icitem.ITEMNO" could not be bound.

I want to insert comment4 from F1$ to comment4 from icitem when ITEMNO is the same.

Thanks

As I understood you need to use update operation instead of insert:

UPDATE dbo.icitem
SET comment4 = f1.comment4
FROM dbo.icitem inner join dbo.f1 on icitem.itemno=f1.itemno

A couple issues:

where dbo.icitem.ITEMNO = dbo.f1$.ITEMNO)

This line is a problem, ITEMNO doesn't have a place in this context. In practice you should be joining the table and doing an update.

Something similar to this should suit your needs according to my understanding:

UPDATE item
SET item.COMMENT4 = f.COMMENT4
FROM dbo.icitem item
    JOIN dbo.f1$ f
        ON f.ITEMNO = item.ITEMNO

This will compare both data sets and then only update rows in icitem where itemno matches between it and the f1$ table

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