title: How to Get all row Parents of mysql table
body: <p>I'm trying to create a stored procedure that returns all of the Id's for the parents of a given id.  Here is what I've got so far, and for all intents and purposes, the table consists of two columns, id and ParentID.  The results of the current code is that it doesn't seem to get the ParentID from the table, it just tries to add the passed in id twice to the temporary table which then fails.  Where did I go wrong?</p>
  
  <pre><code>CREATE PROCEDURE `mydb`.`Test` (IN id int)
  BEGIN
  DECLARE parentId INT;
  DECLARE n INT;
  
    DROP TEMPORARY TABLE IF EXISTS _1067_parents;
  
   CREATE TEMPORARY TABLE _1067_parents (
  node_id INT NOT NULL PRIMARY KEY
    );
  set n = 0;
  set parentId := id;
  
  get_parents_loop: LOOP
       set parentId := (select ParentID from mydb.myTable where TDOID = parentId);
  
      IF parentId is NULL THEN
        LEAVE get_parents_loop;
      END IF;
  
      INSERT INTO _1067_parents(node_id) Values (parentId);
  
  END LOOP get_parents_loop;
  
   SELECT *
      FROM _1067_parents;
  
  end
  </code></pre>