Update a column with values which its parent have.
Say we have a Table (Product)
ProductID, ProductName, IntParentID, IntCategoryID.
With the script below the intCategoryid of Parent will become the intCategoryID of Child.
;with cte as
(
select intCategoryId, intProductID
from Product where intCategoryId is not null and intProductID is not null
union all
select cte.intCategoryId, t.intProductID
from Product t
join cte
on t.intParentCategory = cte.intProductID
and t.intCategoryId is null
)
update t
set t.intCategoryId = cte.intCategoryId
from Product t
join cte
on t.intProductID = cte.intProductID
option (maxrecursion 5000)
ProductID, ProductName, IntParentID, IntCategoryID.
With the script below the intCategoryid of Parent will become the intCategoryID of Child.
;with cte as
(
select intCategoryId, intProductID
from Product where intCategoryId is not null and intProductID is not null
union all
select cte.intCategoryId, t.intProductID
from Product t
join cte
on t.intParentCategory = cte.intProductID
and t.intCategoryId is null
)
update t
set t.intCategoryId = cte.intCategoryId
from Product t
join cte
on t.intProductID = cte.intProductID
option (maxrecursion 5000)
Comments
Post a Comment