I work with a legacy system and there are multiple batches that are executed after one another. In each of these batches there should be a rename of a table, although this rename should only happen once.
So, there could be:
RENAME TABLE oldName TO newName
This has been answered...
With the following code... (all credit to original author)
SELECT Count(*) INTO @exists FROM information_schema.tables WHERE table_schema = [DATABASE_NAME] AND table_type = 'BASE TABLE' AND table_name = 'oldName'; SET @query = If(@exists=0,'RENAME TABLE oldName TO newName','SELECT \'nothing to rename\' status'); PREPARE stmt FROM @query; EXECUTE stmt;
When you don't want to replace [DATABASE NAME] manually you can use the following variable
SELECT DATABASE() INTO @db_name FROM DUAL;