title: MySQL retrieve last_insert_id from stored procedure
body: <p>I am working with NodeJS and MySQL to save a post and its tags via post_tag:</p>
  
  <pre class="lang-sql prettyprint-override"><code>DROP TABLE IF EXISTS post_tag;
  DROP TABLE IF EXISTS post;
  DROP TABLE IF EXISTS tag;
  DROP PROCEDURE IF EXISTS select_all_posts;
  DROP PROCEDURE IF EXISTS insert_post;
  
  CREATE TABLE post (
    id INT NOT NULL AUTO_INCREMENT,
    title VARCHAR(45) NULL,
    body TEXT NULL,
    PRIMARY KEY (id));
  
  CREATE TABLE tag (
    id INT NOT NULL AUTO_INCREMENT,
    tag VARCHAR(45) NULL,
    PRIMARY KEY (id));
  
  CREATE TABLE post_tag (
    post_id INT NOT NULL,
    tag_id INT NOT NULL,
    PRIMARY KEY (post_id, tag_id),
    INDEX fk_post_tag_tag1_idx (tag_id ASC),
    INDEX fk_post_tag_post_idx (post_id ASC),
    CONSTRAINT fk_post_tag_post
      FOREIGN KEY (post_id)
      REFERENCES post (id),
    CONSTRAINT fk_post_tag_tag1
      FOREIGN KEY (tag_id)
      REFERENCES tag (id));
  
  INSERT INTO post (id, title, body) VALUES (1, 'post 1', "Post body");
  INSERT INTO post (id, title, body) VALUES (2, 'post 2', "Post body");
  INSERT INTO tag (id, tag) VALUES (1, 'tag 1');
  INSERT INTO tag (id, tag) VALUES (2, 'tag 2');
  INSERT INTO post_tag (post_id, tag_id) VALUES (1, 1);
  INSERT INTO post_tag (post_id, tag_id) VALUES (1, 2);
  INSERT INTO post_tag (post_id, tag_id) VALUES (2, 1);
  </code></pre>
  
  <p>I created a stored procedure to insert new posts:</p>
  
  <pre class="lang-sql prettyprint-override"><code>-- Stored procedure to insert post and tags
  DELIMITER $$
  CREATE PROCEDURE insert_post(
    IN title VARCHAR(45),
    IN body TEXT
  )
  BEGIN
    INSERT INTO post (title, body) VALUES (title, body);
  END $$
  DELIMITER ;
  </code></pre>
  
  <p>So in node I can do:</p>
  
  <pre class="lang-js prettyprint-override"><code>var mysql = require("mysql2");
  var connection = mysql.createConnection({
    host: "127.0.0.1",
    user: "test_database",
    password: "test_database",
    database: "test_database",
  });
  
  connection.connect();
  
  const postTitle = JSON.stringify("Post 3");
  const postBody = JSON.stringify("This is post 3 body");
  const insertPostQuery = "CALL insert_post(" + postTitle + "," + postBody + " )";
  
  connection.query(insertPostQuery, (error, results) =&gt; {
    console.log(JSON.stringify(results, null, 4));
  });
  
  connection.end();
  
  </code></pre>
  
  <p>This returns:</p>
  
  <pre><code>{
      "fieldCount": 0,
      "affectedRows": 1,
      "insertId": 0,
      "info": "",
      "serverStatus": 2,
      "warningStatus": 0
  }
  
  </code></pre>
  
  <p>Now I want to retrieve the last inserted id from the stored procedure, so I modify my stored procedure to add an <code>OUT last_inserted_id INT</code> param, and then assign it after the INSERT:</p>
  
  <pre class="lang-sql prettyprint-override"><code>-- Stored procedure to insert post and tags
  DELIMITER $$
  CREATE PROCEDURE insert_post(
    IN title VARCHAR(45),
    IN body TEXT,
    OUT last_inserted_id INT
  )
  BEGIN
    INSERT INTO post (title, body) VALUES (title, body);
    SET last_inserted_id = LAST_INSERT_ID();
  END $$
  DELIMITER ;
  </code></pre>
  
  <p>But when executing the query in node this returns <code>undefined</code> instead of the json with the <code>last_inserted_id</code> value. </p>
  
  <p>What I'm doing wrong?</p>