简体   繁体   中英

Using for loop bat file windows for multiple command calls

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?

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM