title: How to insert and update in cursor loop with auto_increment
body: <p>I have attempted looking through many of the topics already and have no success if figuring out what is wrong with my syntax.  I am trying to take the distinct value of one table and loop insert into another table while the id is auto_incrementing.  Here is what I have so far and I can not seem to figure out what I am doing wrong.  I have 2 procedures written.  They both "work" but don't. </p>
  
  <p>Thank you for your help!</p>
  
  <p>Basically I am trying to insert from one table to another, and use the AUTO_INCREMENT to generate my build_id.  The table I want to populate is:</p>
  
  <pre><code>permits.build_type
  
  build_id      INT             PK
  build_desc    VARCHAR(200)
  </code></pre>
  
  <p>and the code is:</p>
  
  <pre><code>DELIMITER //
  
  CREATE PROCEDURE building_list()
  BEGIN
    DECLARE output VARCHAR(200);
    DECLARE my_cursor CURSOR FOR 
          SELECT DISTINCT BUILDING_TYPE 
          FROM permits.edmonton_upto_10may2016_mostly_text;
  
    OPEN my_cursor;  
    BEGIN
      DECLARE EXIT HANDLER FOR NOT FOUND BEGIN END; 
      LOOP
        FETCH my_cursor INTO output; 
        INSERT INTO permits.build_type (build_desc) VALUES (output);
        UPDATE permits.build_type SET build_id = LAST_INSERT_ID();
      END LOOP;
    END;
    CLOSE my_cursor;
  END//
  </code></pre>