Brendan Bishop
Brendan Bishop

Reputation: 1

SQL - Insert into one column within table

so I just added a new column to a table and now i want to populate that column in the pre-existing rows. This my current query, but it keeps erroring, because its trying to insert into the first column of the table.

insert into Product_Database(UPC)
select u.UPC
from upc_temp u
    join upc_temp_gers g on u.PartNumber = g.PartNumber
    join Product_Database p on p.ITM_CD = g.ItemCode
where u.UPC is not null

and this is the error its returning

Cannot insert the value NULL into column 'ITM_CD'

Clearly i am not trying to insert into the 'ITM_CD' column. Just 'UPC'.

Any help would be greatly appreciated. Thanks!

Upvotes: 0

Views: 2056

Answers (3)

Ruchira
Ruchira

Reputation: 135

You have to use UPDATE query for this.

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269503

Presumably the logic you want is:

update p 
    set UPC = u.UPC
    from upc_temp u join
         upc_temp_gers g
         on u.PartNumber = g.PartNumber join
         Product_Database p
         on p.ITM_CD = g.ItemCode
    where u.UPC is not null;

Upvotes: 0

Tab Alleman
Tab Alleman

Reputation: 31775

now i want to populate that column in the pre-existing rows.

After you add a new column to a table, you don't populate the new column for existing rows with an INSERT.

You do it with an UPDATE.

Upvotes: 3

Related Questions