Suppose I have an SQL file commands.sql full of SQL commands. The intention is that the user may run the commands in the file by using the SOURCE command as described here. But it is assumed that the user has selected a database. If the user has not selected a database, then they will see a large number of “No database selected” errors.
Is there a way to use the IF statement to check whether a database is selected, as in
IF a database is selected THEN
(statements)
ELSE
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'You need to select a database';
END IF;
Since scripts abort upon the first error that they encounter (unless invoked with the
forceoption), one can achieve a certain degree of flow-control using prepared statements:(However, I don’t think the
SIGNALcommand can be executed via a prepared statement).