create function f_pid(@id varchar(3)) returns @t_level TABLE(id varchar(3))
as
begin
  insert into @t_level select @id
  select @id = pid from tb where id = @id and pid is not null
  while @@ROWCOUNT > 0
  begin
    insert into @t_level select @id 
    select @id = pid from tb where id = @id and pid is not null
  end
  return
end
go