title: Update table using case when
body: <p>I have the following table <code>test</code></p>
  
  <pre><code>Id     Code        ParentId
  1      R            O
  2      Y            O
  3      P            O
  4      O            NULL
  </code></pre>
  
  <p>I need to update <code>test</code> like that :</p>
  
  <pre><code>   Id     Code        ParentId
      1      R            4
      2      Y            4
      3      P            4
      4      O            NULL
  </code></pre>
  
  <p>I tried that but it doesn't work , any idea ?</p>
  
  <pre><code>update [dbo].[test]
  set [ParentId] =
  CASE WHEN [ParentId]='' THEN [Id]
  else select top 1  [Id] from [dbo].[PNL] where ParentId=[Code]
  End  
  </code></pre>
  
  <p>I got the table <code>test updated</code> </p>
  
  <pre><code>      Id     Code        ParentId
          1      R          NULL
          2      Y          NULL
          3      P          NULL
          4      O          NULL
  </code></pre>