MSSQL-使用自查找更新表

MSSQL-使用自查找更新表,sql,sql-server,Sql,Sql Server,因此,我有一个表如下 id, title, application_number, parent_application_number, parent_case_id 2, 'asd', 'P1234', 'lkjh', null 3, 'qwe', 'adsqwe', 'P1234', 3 我知道这看起来令人困惑,但我会尽我所能解释它。 我基本上需要查看当前行的parent\u case\u id,并进行查询以找出哪一行将此字段作为应用程序编号,并将该id更新为parent\u case\u

因此,我有一个表如下

id, title, application_number, parent_application_number, parent_case_id
2, 'asd', 'P1234', 'lkjh', null
3, 'qwe', 'adsqwe', 'P1234', 3
我知道这看起来令人困惑,但我会尽我所能解释它。 我基本上需要查看当前行的parent\u case\u id,并进行查询以找出哪一行将此字段作为应用程序编号,并将该id更新为parent\u case\u id

到目前为止我有这个,有人能给我指一下正确的方向吗

UPDATE guest.exported_cases
    set parent = 
          (CASE a.child
               WHEN '' THEN ''
               WHEN NULL THEN ''
               else (select id from guest.exported_cases b where b.child = a.child)
          END)
    FROM guest.exported_cases a;

提前谢谢

您可以使用
联接
查找每个子级的父行:

update child
set    parent = parent.id
from   YourTable child
join   YourTable parent
on     parent.child = child.id
where  child.parent is null -- Does not currently have a parent

这将基于父级的子列设置子级的父列。

抱歉,我在发布时出错。我现在将更新OP。