How can I make MySQL throw an error (not simply issue a warning) when I try and perform a mathematical operation with text in the SELECT list. For example:
SELECT 'foo' * 'bar';
I want that query to fail.
How can I make MySQL throw an error (not simply issue a warning) when I try and perform a mathematical operation with text in the SELECT list. For example:
SELECT 'foo' * 'bar';
I want that query to fail.
2
Answers
You could define a function like this:
Then
select multiply('foo','bar');
will return an error, whileselect multiply(2,3);
still returns the correct answer.But the downside is that you need to use this function in stead of the normal way of multiplying.
For data altering statements (INSERT/UPDATE/etc), these types of operations will already produce an error, assuming you have the STRICT_TRANS_TABLES sql_mode enabled, which has been the default since mariadb 10.2.4 and mysql 5.7.5.
For non-data altering statements, have your client check if there was a warning; details depend on what client you are using.