I was wondering if there's such thing as the equivalent as a callback function using mysql after an INSERT or UPDATE which could return me the row # and maybe values of such rows.
-
3how and where do you need this values to be returned? – Davide Piras Aug 28 '11 at 11:39
-
Are you trying to [simulate the `OUTPUT` clause](http://stackoverflow.com/q/5817414/73226)? – Martin Smith Aug 28 '11 at 11:40
-
see this as well: http://stackoverflow.com/questions/933565/get-auto-increment-value-with-mysql-query – Davide Piras Aug 28 '11 at 12:14
-
***[`Triggers`](https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html)*** – ashleedawg Dec 29 '19 at 18:24
3 Answers
You can create triggers that are called on insert and update. They do not return value, but they can set variables you can read outside them.

- 26,265
- 5
- 59
- 89
-
1Thank you! I got here by Google searching for "MySQL callback" in order to find the term "trigger", as I'm sure many others have done. – maurice Jul 28 '17 at 00:35
I am not aware of any callback as you say but surely from your calling application you can retrieve the last inserted ID in case you did not specify it and the db has generated an auto increment value. Other values you should already know because you have inserted them.
if you need to know those values within the database server, you can have a SQL trigger which is executed at every insert so you can do more processing on the newly inserted record, for example write something in another table etc...

- 43,984
- 10
- 98
- 147
-
how can I get that last ID? because an insert query doesn't return (as far as I know) a value besides success or failure. – Itai Sagi Aug 28 '11 at 12:00
-
http://stackoverflow.com/questions/933565/get-auto-increment-value-with-mysql-query – Davide Piras Aug 28 '11 at 12:13
-
1@Itai Sagi call [last_indert_id())](http://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_last-insert-id) – Maxim Krizhanovsky Aug 28 '11 at 12:15
This isn't available in MySQL as it stands. I think there are two methods to achieve this:
- You would have to simulate it with polling - a rather ugly method, easy on the programming but tough on the server.
- See Does MySQL permit callbacks in C such that when a change happens, I can be notified? - write a user defined function that could notify a registered listener using some proprietary method of your devising. Tougher on the programmer, easy on the server. In this case make sure your UDF is robust, doesn't freeze while passing a notification to a listener who may have died, etc.
See this for a Firebird event description in case you (or some later reader) decides to do it via UDF - it's a good design spec to aim at: http://www.janus-software.com/fbmanual/manual.php?book=php&topic=49

- 1
- 1

- 369
- 4
- 13