0

By seeing below example, how can I update ORIGINAL_ORDER_NO null value with an already existing value (ex with: 9007853627), and also update original_order_line_no number with equivalent ORDER_LINE_NO?

I will pass the ORDER_NO and script needs to updates rest of it.

ORIGINAL_ORDER_NO   original_order_line_no  ORDER_NO    ORDER_LINE_NO   
---------------------------------------------------------------------
9007853627             1                    9008142190   1              
9007853627             2                    9008142190   2  
NULL                  NULL                  9008142190   4   
NULL                  NULL                  9008142190   5  

Desired output:

 ORIGINAL_ORDER_NO  original_order_line_no  ORDER_NO    ORDER_LINE_NO   
-----------------------------------------------------------------------
    9007853627             1                9008142190   1              
    9007853627             2                9008142190   2  
    9007853627             4                9008142190   4   
    9007853627             5                9008142190   5  

2 Answers 2

3

ISNULL is one way, and @yourParam here is what you are passing in for ORDER_NO

update yourTable
set   original_order_line_no   = isnull(original_order_line_no,ORDER_LINE_NO)
     ,ORIGINAL_ORDER_NO   = (select top 1 ORIGINAL_ORDER_NO from yourTable where ORDER_NO = @yourParam)  
where ORDER_NO = @yourParam and ORIGINAL_ORDER_NO is null
Sign up to request clarification or add additional context in comments.

1 Comment

9007853627 != 9008142190
1

You could use:

WITH cte AS (
  SELECT *, MIN(ORIGINAL_ORDER_NO) OVER(PARTITION BY ORDER_NO) as s
  FROM tab
  WHERE order_no = ?
)
UPDATE cte
SET ORIGINAL_ORDER_NO = s
   ,original_order_line_no = ORDER_LINE_NO  
WHERE ORIGINAL_ORDER_NO IS NULL;

DBFiddle Demo

4 Comments

Where can I check with ORDER_NO?
@James123 Did you even try it?
dbfiddle.uk/….. If you see this I don't want update all orders. I want update particular order.
@James123 Just simply add where clause demo2

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.