2

I want to export all data from sql server table to a csv, I know I can get the desired result by:

sqlcmd -S . -d database -E -s, -W -Q "SELECT * FROM TABLENAME" > file.csv

I have many tables, so I want to create a .bat file that do the work for me, I have this:

set "list = A B C D"


for %%x in (%list%) do (
    sqlcmd -S . -d database -E -s, -W -Q "SELECT * FROM %%x" > %%x.csv

)

But I am getting errors I don't know (I am not an expert in bat files). Why this does not work? How can I do what I want?

dpalma
  • 500
  • 5
  • 20
  • 1
    Possible duplicate of [Declaring and using a variable in DOS/Windows batch file (.BAT)](http://stackoverflow.com/questions/10552812/declaring-and-using-a-variable-in-dos-windows-batch-file-bat) – aschipfl May 03 '16 at 16:03

1 Answers1

6

Spacing is important when using set (unless you're doing math with the /A switch). As written, the variable you're setting isn't %list%. It's %list %. Change your set command as follows:

set "list=A B C D"
rojo
  • 24,000
  • 5
  • 55
  • 101