how to get the ID of an insert SQL with Execute ?

hi,

in an server event, i perform a SQL insert query with Execute Command from the API.
Is there a way to get the ID of what i inserted ?

Actualy, i have to do another SQL query into my table to get the last insert id !

i checked the content of $myResult = Execute($sql) but $MyResult did not contain the last_insert_id …

Best regards

If you check the result of your INSERT statement, there is no last insert id. I’m not sure which “API” you referred to (Execute() is just a global function), but if you use the REST API’s “add” action to insert, the result is like { “success”: true, “version”: “15.0.0”, “cars”: { “ID”: 16, “Trademark”: 1, … } } which contains the new ID, see the topic REST API in the help file. If you Execute(), you do need another query to get the last insert id as required by databases.

hi,

i looked at the help file, event section.
i saw example of codes using Execute command, or ExecuteRow… so i learnt and re-use it.
My need is server-side.
My event is Row_Updated, so i just need to insert something in another table once all is updaded and i need to get the last id.

So now, i do 2 query :
one is : $result=Execute (“insert into other_table …)”;
second is : $result=ExecuteRow (“select max(id) from other_tables…”); and read the $result[0] to get my last id

i just want to do only one query …

  1. arbei wrote:

If you Execute(), you do need another query to get the last insert id as required by databases.

  1. You should not use ‘max(id)’, you may use, e.g.

Conn()->Execute(“INSERT …”);
$lastInsertId = Conn()->Insert_ID();