I want to run an update query.
The query will be run against multiple databases – not every database will have the table.
I don’t want the update to be attempted if the table does not exist. I don’t want any error to be thrown – I just want the update to be ignored.
Any ideas?
EDIT: just to be clear – the query is executed in an automated deployment – no human interaction possible.
EDIT2: the logic to say whether the update should run or not will need to be in the MySql query itself. This is not being run through a command prompt or batch or managed code.
To do this in native MySQL (like in a mysql script), you could use a stored procedure.
This would be appropriate for a one-off administrative type function, you wouldn’t want to do this as part of an application. (Then again, if this were part of an application, you would have the conditional logic to control whether an UPDATE is performed, or to catch and handle the exception if the table doesn’t exist.)
Here’s an example of a stored procedure that uses a
CONTINUE HANDLERto catch error 1146 (table does not exist) and swallow it, so that no error is returned to the caller.Another option is to test for the existence of the table, and conditionally running the statement. In native MySQL, again, this would need to be done inside a stored procedure.
My preference would be to go with the
CONTINUE HANDLER.