Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用列中的现有值更新列中的null_Sql_Sql Server - Fatal编程技术网

Sql 使用列中的现有值更新列中的null

Sql 使用列中的现有值更新列中的null,sql,sql-server,Sql,Sql Server,通过下面的示例,我如何使用现有值(例如:9007853627)更新ORIGINAL\u ORDER\u NO空值,以及如何使用等效的ORDER\u line\u NO更新ORIGINAL\u ORDER\u NO编号 我将传递订单号,脚本需要更新其余部分 ORIGINAL_ORDER_NO original_order_line_no ORDER_NO ORDER_LINE_NO -------------------------------------------------

通过下面的示例,我如何使用现有值(例如:
9007853627
)更新
ORIGINAL\u ORDER\u NO
空值,以及如何使用等效的
ORDER\u line\u NO
更新
ORIGINAL\u ORDER\u NO
编号

我将传递
订单号
,脚本需要更新其余部分

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  
期望输出:

 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  

ISNULL
是一种方式,@yourParam这里是您传递的订单号

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

ISNULL
是一种方式,@yourParam这里是您传递的订单号

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
您可以使用:

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;

您可以使用:

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;

9007853627
!=<代码>9008142190
9007853627
!=
9008142190
我在哪里可以查看订单号?@James123你试过吗?。。如果你看到这个,我不想更新所有订单。我想更新特定的顺序。@James123只需添加where子句,我可以在哪里使用
order\u NO
?@James123您尝试过吗?。。如果你看到这个,我不想更新所有订单。我想更新特定的顺序。@James123只需添加where子句即可