I'd like to store warnings caused by some SQL statements in the database. E.g. after
mysql> select 1/0;
+------+
| 1/0 |
+------+
| NULL |
+------+
1 row in set, 1 warning (0.00 sec)
mysql> show warnings;
+-------+------+---------------+
| Level | Code | Message |
+-------+------+---------------+
| Error | 1365 | Division by 0 |
+-------+------+---------------+
1 row in set (0.00 sec)
I'd like to save what SHOW WARNINGS says to some persistent table, to be able to analyze it in future.
Let's assume I know when there is something to log - I can check if @@warning_count > 0
.
The mysql client also has a number of options related to warnings. The \W command will show warnings after every statement, while \w will disable this. Starting the client with the --show-warnings option will show warnings after every statement.
SHOW WARNINGS is a diagnostic statement that displays information about the conditions (errors, warnings, and notes) resulting from executing a statement in the current session.
MySQL uses security based on Access Control Lists (ACLs) for all connections, queries, and other operations that users can attempt to perform. There is also support for SSL-encrypted connections between MySQL clients and servers.
To suppress warnings, set SQL_NOTES=0.
You can accomplish this within a stored procedure by analyzing the diagnostics. For example, the following handler for warnings could be declared by the following
DECLARE CONTINUE HANDLER FOR SQLWARNING
BEGIN
GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE,
@errno = MYSQL_ERRNO, @message = MESSAGE_TEXT
;
/* select the following into your debugging table */
SELECT @errno, @sqlstate, @message;
END;
More information about DIAGNOSTICS can be found here: https://dev.mysql.com/doc/refman/5.6/en/get-diagnostics.html
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