How can I make a stored procedure to print a specific message if an empty set wa returned by the query?
Mysql – Stored procedure to handle empty sets
MySQLstored-procedures
Related Question
- Sql-server – How to search the full text of a stored procedure for a value
- Mysql – How to access a result set from a stored procedure in another stored procedure
- Sql-server – Alter of stored procedure and execution performance
- Sql-server – Stored Procedure RAISERROR – Transaction Count Msg Returned
- PostgreSQL – Conditional WHERE for Stored Procedure
- Oracle – How to Create Stored Procedure That Returns Query Result
- SQL Server – Return Recordset from Stored Procedure Only if It Has Rows
- Sql-server – MS SQL Server 2014: which stored procedure is the error in
Best Answer
If you are using MySQL 5.5 or 5.6, you can accomplish this with
SIGNAL
.Simplified example, inside the body of the procedure...
This will throw an exception:
If you wanted to be more specific, you'd need to declare a variable to use a the
MESSAGE_TEXT
and populate it accordingly before callingSIGNAL
becauseSIGNAL
only accepts a variable -- not an expression -- as an argument:Your code would probably take a different approach than
IF ... EXISTS
such as selectingINTO
a variable and then testing that variable for null and then either returning it to the called with an unboundedSELECT
or throwing the exception.The reason for the
LEFT( ...,128)
is that this is the maximum length for an argument toMESSAGE_TEXT
. Overrunning it won't hurt anything but you'll end up throwing an error about the invalid argument instead of the error you intended.For simplicity, the example assumes that the password is in cleartext which obviously wouldn't be the case.