亲子关系更新

时间:2019-11-22 10:00:44

标签: sql sql-server

我有以下数据:

  ParentElem    ChildElem        Id                                 ParentElemId
  Core Sub      SDG-1       f2e3e594-92a7-44a5-9593-47381f29db10    NULL
  Core Sub      Rea 2       bfca0601-edbb-40aa-a360-f7381db888ef    NULL
  Core Sub      Rea 1       3c584b87-0f57-46c2-9819-eb103305e7f3    NULL
  Core Sub      PDM 3       c7b924b9-a93b-4db1-bd29-82b960ecb699    NULL
  Core Sub      PDM 2       4d0a3675-d526-4bea-86bc-d43678205dca    NULL
  Core Sub      Switchgear  41aba4f3-903f-4951-82d7-82093b004ffd2   NULL
  Distribution  Core Sub    5f528c32-87be-47b5-8e0f-6279d87dacf0    NULL
  Distribution  NPS         3b714bd2-69d8-4f43-95d8-10ce460325fc    NULL
  NPS           Switchgear  41aba4f3-903f-4951-82d7-82093b004ffd3   NULL
  Switchgear    BB1         d8aeabf0-f58f-4b79-b35b-3b8917eb41d1    NULL
  Switchgear    Interconnec d69ef085-c7ce-4129-90ef-00cfc4b92b6e    NULL
  Switchgear    Non-Busbar  93306313-996e-4d58-a978-7b91f2122ab0    NULL
  Switchgear    Panel 11    5ac4c79b-1673-4fc1-9a9a-a3697ae5c754    NULL
  Switchgear    BB2         f90ae7b4-d5f9-4047-b483-a41b0e2381cd    NULL

我想得到以下消息:

ParentElem  ChildElem        Id                                 ParentElemId
  Core Sub      SDG-1       f2e3e594-92a7-44a5-9593-47381f29db10    NULL
  Core Sub      Rea 2       bfca0601-edbb-40aa-a360-f7381db888ef    NULL
  Core Sub      Rea 1       3c584b87-0f57-46c2-9819-eb103305e7f3    NULL
  Core Sub      PDM 3       c7b924b9-a93b-4db1-bd29-82b960ecb699    NULL
  Core Sub      PDM 2       4d0a3675-d526-4bea-86bc-d43678205dca    NULL
  Core Sub      Switchgear  41aba4f3-903f-4951-82d7-82093b004ffd2   5f528c32-87be-47b5-8e0f-6279d87dacf0
  Distribution  Core Sub    5f528c32-87be-47b5-8e0f-6279d87dacf0    NULL
  Distribution  NPS         3b714bd2-69d8-4f43-95d8-10ce460325fc    NULL
  NPS           Switchgear  41aba4f3-903f-4951-82d7-82093b004ffd3   3b714bd2-69d8-4f43-95d8-10ce460325fc
  Switchgear    BB1         d8aeabf0-f58f-4b79-b35b-3b8917eb41d1    NULL
  Switchgear    Interconnec d69ef085-c7ce-4129-90ef-00cfc4b92b6e    NULL
  Switchgear    Non-Busbar  93306313-996e-4d58-a978-7b91f2122ab0    NULL
  Switchgear    Panel 11    5ac4c79b-1673-4fc1-9a9a-a3697ae5c754    NULL
  Switchgear    BB2         f90ae7b4-d5f9-4047-b483-a41b0e2381cd    NULL

因此,正如您所看到的,Switchgear是Core Sub的ChildElem,我需要将ParentElemId设置为Core Sub的ID(即Distribution的ChildElem)。 对于另一个Switchgear,也是如此,它是NPS的ChildElem,我需要将ParentElemId设置为NPS的ID(即分发的ChildElem)。 这只是示例,应使用上面的规则设置ParentElemId的其余部分。

我尝试了以下查询:

 update [Hierarchy2] set [ParentElemId] = (select Id from [Hierarchy2_copy] where
 [Hierarchy2].[ChildElem]=[Hierarchy2_Backup].[ParentElem]) 

但它抱怨以下错误:

Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= 
or when the subquery is used as an expression.
The statement has been terminated.

0 个答案:

没有答案