简体   繁体   English

使用for循环bat文件窗口进行多个命令调用

[英]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: 我想将所有数据从sql server表导出到csv,我知道我可以通过以下方式获得所需的结果:

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: 我有很多表,所以我想创建一个.bat文件来为我完成工作,我有这个:

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). 但是我遇到了我不知道的错误(我不是bat文件的专家)。 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). 使用set时,间距很重要(除非使用/A开关进行数学运算)。 As written, the variable you're setting isn't %list% . 按照书面规定,您要设置的变量不是%list% It's %list % . 这是%list % Change your set command as follows: 更改您的set命令,如下所示:

set "list=A B C D"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Bat文件-使用Windows进行身份验证 - Sql Bat File - authenticate using Windows 使用单个bat文件一次性执行多个文件 - Execute multiple files at one go using a single bat file .BAT文件代码出现在控制命令上时 - .BAT file code on when control command appears 无法使用SOURCE命令运行sql文件的Windows命令行 - can not run a sql file windows command line using SOURCE command bat file命令,用于将精确的plsql查询输出写入到csv文件中 - bat file command for Write exact plsql query output into csv file 使用bat文件调用sql脚本 - Call sql script using bat file 使用BCP命令运行bat文件,出现连接错误 - Running a bat file with BCP command, getting connection error 如何将参数从bat文件或命令行传递到plpgsql函数? - How to pass parameters from a bat file or a command line to a plpgsql function? 在bat文件中运行sql脚本,该bat文件将调用其他3个sql脚本 - Running a sql script inside a bat file which calls 3 other sql scripts 在 Visual Basic 中使用 Windows 窗体在一个命令中使用多个 SQL 插入语句将多行插入到表中, - Using multiple SQL insert statements in one command to insert multiple rows to a table using a windows form in Visual Basic,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM