1

I have an sql file with certain parameters which I need to make as generic ones.

Below is the code base in C:\sql\main.sql:

SET @NAME = 'XYZ'

source C:/abc.sql

I need all the occurences of NAME to be replaced by XYZ in abc.sql file.Which basically contains a set of insert statements.I ran the below command :

mysql -u myusername -p DB < C:\sql\main.sql

But after executing it throws and error saying:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SOURCE C:/abc' at line 2

All I need to do is call an sql file C:\abc.sql in C:\sql\main.sql and then execute C:\sql\main.sql . Can anyone help me on this?

2
  • The error message looks truncated as there is no closing single quote, please check if it is complete and attach the full message if not. Commented Apr 13, 2016 at 16:16
  • yes just modified it. I am wondering if this is even possible to achieve? Commented Apr 13, 2016 at 16:25

1 Answer 1

2

I just tested this and I think the only problem with your script (main.sql) is the statements are not terminated with semicolons.

Try adding a semicolon to the end of the line:

SET @NAME = 'XYZ';

I was able to verify that the user-defined variable @NAME is in-scope when the second file (abc.sql) executes with the following in the second file:

SELECT @NAME;

# Output:
XYZ
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer . one of my insert statements is as below: INSERT INTO Table_One(name) values('JOHN_'\@NAME\'_SMITH'); while executing it throws below error : `ERROR 1048 (23000) at line 5 in file: 'C:/abc.sql': Column 'Name' cannot be null'. How to place @NAME in between the NAME value JOHN SMITH? It should look like JOHN_XYZ_SMITH
Try using CONCAT(), like CONCAT('JOHN_', @NAME, '_SMITH') - also, for additional questions you should post as a new question, not as a comment.

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.