Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

T-SQL UPDATE query to fill NULL columns

I have the following query:

SELECT 
    Quotes.quoteID, Orders.orderID, Orders.invoiceID, 
    Quotes.parentOrderID AS QuoteParent, Orders.parentOrderID AS OrderParent
FROM 
    Quotes 
INNER JOIN 
    Orders ON Quotes.orderID = Orders.orderID

I need to fill all the OrderParent columns with valid (not null) values in QuoteParent column.

How do I write the UPDATE query?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

How about:

UPDATE o
SET parentOrderID = q.parentOrderID
FROM dbo.Orders o
INNER JOIN Quotes q ON o.OrdersID = q.OrderID
WHERE o.parentOrderID IS NULL
  AND q.parentOrderID IS NOT NULL
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading