0

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!

1
  • 1
    Have you looked at the table definition of Product_Database? I'm certain there is a not null constraint on the column ITM_CD and you are not providing any value for ITM_CD. Hence the row would have NULL as a value for ITM_CD, which the constraint prevents. Commented Oct 18, 2018 at 19:50

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;

Comments

0

You have to use UPDATE query for this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.