Dim insert_coupon_query As String = ("INSERT INTO qa_discountcoupons (id, status_code) VALUES (AUTO_INCREMENT_ID, 5)")
Dim cmd_query As New MySqlCommand(insert_coupon_query, objConn)
Dim cmd_result As Integer = CInt(cmd_query.ExecuteScalar())
I want to return the AUTO_INCREMENT value of the current insert, and show in a msgbox.
MySQL 8.0 C API Developer Guide / ... / Obtaining the Unique ID for the Last Inserted Row If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id () function.
select last_insert_id (); Code language: SQL (Structured Query Language) ( sql ) As you can see clearly from the output, the LAST_INSERT_ID() function returns the generated value of the first row successfully inserted, not the last row.
Let’s look at an example of using MySQL LAST_INSERT_ID function. First, create a new table named messages for testing. In the messages table, we set the AUTO_INCREMENT attribute for the id column. Second, insert a new row into the messages table. Third, use the MySQL LAST_INSERT_ID function to get the inserted value of the id column:
Code language:SQL (Structured Query Language)(sql) Second, insert a new row into the messagestable. INSERTINTOmessages(description) VALUES('MySQL last_insert_id'); Code language:SQL (Structured Query Language)(sql) Third, use the MySQL LAST_INSERT_IDfunction to get the inserted value of the idcolumn: SELECTLAST_INSERT_ID();
You can use an double Query and use the function LAST_INSERT_ID() After run your first query to get the last current query:
Dim insert_coupon_query As String = ("INSERT INTO qa_discountcoupons (status_code) VALUES (5); SELECT LAST_INSERT_ID()")
Dim cmd_query As New MySqlCommand(insert_coupon_query, objConn)
Dim cmd_result As Integer = CInt(cmd_query.ExecuteScalar())
MsgBox(cmd_result)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With