I am trying to run a batch script that writes out a bunch of text to a .sql file that gets run in a SQL Command window. I am trying to have my batch script output a line of text that says:
insert into TEST_TABLE (param1, param2) values ('JohnSmith','Test1');
My code below as is does not work, CMD just crashes immediately upon running the .bat file:
@echo off
(
echo insert into TEST_TABLE (param1, param2) values ('JohnSmith','Test1');
) > TEST.sql
pause
However, when I change it and add quotes:
@echo off
(
echo "insert into TEST_TABLE (param1, param2) values ('JohnSmith','Test1');"
) > TEST.sql
pause
When I do this, it outputs the file as expected with the line of text in the file, however it has the quotes which I cannot have in there because that will cause SQL command to fail.
Can anyone please help me understand how to get this script to output this specific text to a .sql file without the quotes?
Thanks
You have two options here. Use set /P
command instead of echo
to enclose the text in quotes (and show it with no quotes):
@echo off
< NUL (
set /P "=insert into TEST_TABLE (param1, param2) values ('JohnSmith','Test1');"
echo/
) > TEST.sql
pause
... or escape each right parentheses with a caret:
@echo off
(
echo insert into TEST_TABLE (param1, param2^) values ('JohnSmith','Test1'^);
) > TEST.sql
pause