MySQL script with parameters

I want to create a deployment script, somehow emulate Oracle deployment scripts, where with the & param parameter you can use the previously declared parameters. I need to call this script for different users in different databases automatically.

For example, my script should be:

USE &param; DROP TABLE IF EXISTS `TEST` ; CREATE TABLE IF NOT EXISTS `TEST` (X INT(16)) etc.... 

Of course, param is what I would use in an Oracle environment.

thanks

Update:

I forgot to mention that I am currently using the Windows environment. I created a script package to call a mysql script. The easiest way, in my opinion, is to go to the mysql 2 command: 1) use the schema that I have as a parameter, and then call the script, which will create the table regardless of the schema. Unfortunately, mysql seems to understand that I want to connect to the X schema, but I don't want to call the script.

 REM param is the schema and mainsql is the script SET param="%1" SET mainsql="script.sql" echo %param% echo %mainsql% mysql -u <user> --password=<psw> %param% "source %mainsql%;" 
+4
source share
1 answer

As far as I know, you cannot pass variables directly to a MySQL script. The best you can do is set custom variables in a shell shell script. Sort of:

 passed_var1=$1 passed_var2=$2 mainsql=script.sql mysql $(usual_parameters) -e "set @user_var1=$passed_var1; set @user_var2=$passed_var2; source $mainsql" 

Adjust for actual use, of course.

+5
source

Source: https://habr.com/ru/post/1403906/


All Articles