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?

1

1 Answer 1

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"
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.