0

I need to populate the contents of a SQL Server table with the contents of another.

I have one table, Document Items, which contains (say) VendorPartNumber and UnitCost columns.

I then have another table, PO Items, with VendorPartNumber and UnitCost.

What do I need to do to get the relevant column contents from DocumentItems into PO Items?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Matt Rose
  • 9
  • 1

3 Answers3

1
update dbo.[PO Items]

set 
    VendorPartNumber = di.VendorPartNumber,
    UnitCost = di.UnitCost
from DocumentItems di 

where [PO Items].[{key column name}] = di.[{key column name}]
Max Sorin
  • 1,042
  • 6
  • 14
0

If it is for one time operation:

If Part Oder Number is unique key in both tables, you can use the sub select statement from source table and set the values to 2nd table.

Ex:

update PO Items 
set VendorPartNumber = (select VendorPartNumber 
                        from Document Items 
                        where partOrdernumer = ?), 
    UnitCost = (select UnitCost 
                from Document Items 
                where partOrdernumer = ?) 
where partOrdernumer = ?

Else use triggers to update second table.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
-1

You can try with:

insert into POItems (VendorPartNumber, UnitCost) values (select VendorPartNumber, UnitCost from DocumentItems); 

You can add where clause if you need.

Claudio
  • 642
  • 3
  • 12