with UdateProd(P1,P2,C) as
(
select product_id,Parent_Product_Id,isnull(Is_Client_prod,0) C from prod
Union All
select P1,Parent_Product_Id,C from UdateProd join prod on p2 = product_id
) select P2,Max(C) C into #t from UdateProd where p2 is not null group by P2 order by 1,2