1

I am trying to pass parameters to my sql script via cmd on Windows but it does not work nor do I get any errors though.

This will be running on a Windows environment and executed using cmd. I have the following code in my deploy.sql script:

INSERT INTO `database`.`users` (`id`, `first_name`, `last_name`, `username`, `active`)
    VALUES (1, @first_name, @last_name, @user_name, 1);

I want to pass 3 strings to my script but it does not work. If I hard code a the strings in the script then I can execute the script via cmd perfectly.

The command I am trying to run to pass the parameters and execute the script is:

"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe" -u root -p -e "set @first_name='John'; set @last_name='Smith'; set @user_name='john.smith';" < deploy.sql 

My database is not updating nor do I get any errors. If I run the command with -v for a verbose output I get:

--------------
set @first_name='John'
--------------
--------------
set @last_name='Smith'
--------------
--------------
set @user_name='john.smith'
--------------

1 Answer 1

2

I solved my problem by using \. deploy.sql inconjunction with -e instead of specifying my script file like < deploy.sql.

Here is the working code:

"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe" -u root -p -e "set @first_name='John'; set @last_name='Smith'; set @user_name='john.smith'; \. deploy.sql" 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.