Home  >  Q&A  >  body text

How to batch insert inserted ids in mysql

I am trying to get the insertid after inserting multiple rows in mysql table using this query

insert into sometable (id , candidateid , createdby)
    values ('61','3175','1425'),('60','3175','1425'),('42','3175','1425'),('61','3176','1425'),('60','3176','1425'),('42','3176','1425') OUTPUT INSERTED.id

But I get sql syntax error

code: 'ER_PARSE_ERROR',
  errno: 1064,
  sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OUTPUT INSERTED.id' at line 2",
  sqlState: '42000',

How to get the insertion ID

P粉738248522P粉738248522229 days ago490

reply all(1)I'll reply

  • P粉614840363

    P粉6148403632024-03-27 00:53:20

    OUTPUT INSERTED The syntax is Microsoft SQL Server Thing. MySQL does not support this syntax.

    In your example, you have explicitly set the id values, so there is no need to return them. Just get them from your own values, which you obviously already know before performing the insertion.

    You may rely on the % generated during insert

    reply
    0
  • Cancelreply