I have an SQL procedure that calls the command shell to write an SQL dump to a file. Take this redacted file for example:
VALUES(
@SomeVar
,1
)
The procedure calls:
echo VALUES(>> C:\somefile.sql
echo @SomeVar>> C:\somefile.sql
echo ,1>> C:\somefile.sql
echo )>> C:\somefile.sql
This works fine, except for the ,1 line. If you run echo ,1>> C:\somefile.sql in CMD, you will see that C:\somefile.sql only contains ,
My theory is that echo thinks it can accept more than 1 parameter.
If you modify the command to echo ,1blah>> C:\somefile.sql, it works perfectly.
I could modify my procedure to check if the line contains a , followed by a number, not followed by anything, and prepend ^ to the number to escape it. This is a bit of a pain though.
Also, echo 1>> C:\somefile.sql writes Echo is ON
Is there a way to echo a literal string in CMD? Enclosing the string in " " outputs the " marks. Or is there any other solution you can think of?
The problem is that
1is the file descriptor for the standard output stream in batch files /CMD.EXE. You can work around this by using this line instead:Or, but more fragile, just put a space between the
1and the>>redirectionUpdate: If you are really concerned about the trailing whitespace for the above examples, you could also use
I.e. you can also put the redirection in front of the command. Personally, I think it looks a little awkward, but YMMV.