Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick SQL code help dealing with NULLS 1

Status
Not open for further replies.

grippy

Programmer
Dec 2, 2004
161
US
Code:
UPDATE ProductVariant
SET Length = (SELECT Table1.Length
FROM Table1
WHERE ProductVariant.Sku = Table1.Sku)

I keep getting this NULL error code but cant figure it out. There are no nulls in Table1 is there something I should add to the code to make this work? Thank you.

Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'Length', table 'database.dbo.ProductVariant'; column does not allow nulls. UPDATE fails.
The statement has been terminated
 
there's at least one row in Table1 that doesn't have a matching sku, and in that case the subquery returns NULL

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Oh ok that is why then because there is a lot of data in table1 but not all of the skus exist in ProductVariant table. What would I need to change on my code to counteract that ? Thank you for your help.
 
Code:
UPDATE ProductVariant
   SET Length = t1.Length
  FROM ProductVariant pv
INNER
  JOIN Table1 t1
    ON t1.Sku = pv.Sku

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top