我有一个 MySQL 脚本,我想仅在数据库不存在时使用它来注入一些用于开发的初始演示数据.如果它确实存在,我只想跳出脚本.脚本开始像
I have a MySQL script that I want to use only if the database doesn't exist to inject some initial demo data for development. If it does exist I just want to break out of the script. The script starts like
CREATE DATABASE IF NOT EXISTS `demo-database`; USE `demo-database`;如果数据库存在,有没有办法在创建数据库的这里或上面退出,这样它就不会运行所有的表设置和插入?
Is there a way to exit here or above the create database if the database exists so that it wont run through all the table setups and inserts?
推荐答案我现在相信带有 if/else/endif 的构造只能在存储程序中使用(另请参见 dev.mysql/doc/refman/8.0/en/if.html)
What I believe now is that the construct with an if/else/endif can only be used in a stored program (see also dev.mysql/doc/refman/8.0/en/if.html)
除了创建包含代码的存储过程之外,我没有其他解决方法.在我的情况下,我也有只在测试环境中执行的代码.我也只在测试中创建存储过程.
I do not have a workaround other than creating a stored procedure that contains the code. In my situation I also have code I would to execute only on a test environment. I am creating only the stored procedure on test then as well.
更多推荐
如果数据库存在则退出 MySQL 脚本
发布评论