SQLTeam.com | Weblogs | Forums

Update Column Null


#1

I have Table Contain
ID Code ParentID
1 NULL 0
2 NULL 1
3 NULL 1
4 NULL 2
5 NULL 3
6 NULL 0
7 NULL 6
8 NULL 7

I Need Update Column "Code"
The Result
ID Code ParentID
1 ---- 001 ----------- 0
2 ---- 001001 ----- 1
3 ---- 001002 ----- 1
4---- 001001001-- 2
5---- 001002001-- 3
6---- 002 ---------- 0
7---- 002001 ------ 6
8---- 002001001-- 7


#2
DECLARE @t table(ID int, Code varchar(20) NULL, ParentID int)
INSERT @t
   (  ID
    , Code
    , ParentID
   )
VALUES
   ( 1, NULL, 0 )
 , ( 2, NULL, 1 )
 , ( 3, NULL, 1 )
 , ( 4, NULL, 2 )
 , ( 5, NULL, 3 )
 , ( 6, NULL, 0 )
 , ( 7, NULL, 6 )
 , ( 8, NULL, 7 );
UPDATE @t
SET Code = CASE ID
              WHEN 1 THEN '001'
              WHEN 2 THEN '001001'
              WHEN 3 THEN '001002'
              WHEN 4 THEN '001001001'
              WHEN 5 THEN '001002001'
              WHEN 6 THEN '002'
              WHEN 7 THEN '002001'
              WHEN 8 THEN '002001001'
          END ;
SELECT t.ID
     , t.Code
     , t.ParentID
FROM @t t;

image


#3

thank you jotorre_riversidedpss.org
but ID and ParentID is Unknown