| 
 | 
create function get_root(@id varchar(10)) 
returns varchar(10) 
as 
begin 
   declare @str varchar(10) 
   declare @i varchar(10) 
   select @str=id,@i=par from tablename where id=@id 
   while @i<>'0' 
         select @str=id,@i=par from tablename where id=@i 
   select @str=root from tablename where id=@str 
  return(@str) 
end 
go 
select dbo.get_root('7') 
update tablename set root=dbo.get_root(id) |   
 
 
 
 |