【发布时间】:2020-06-03 05:18:38
【问题描述】:
我正在尝试运行一个批处理脚本,它将从 SQL Server 表结果集中获取值,并将其作为循环中的输入传递给批处理脚本中的 .EXE 并递归运行。
示例:
SQL Query:
select Name from sys.tables
SQL Query output:
a
b
c
/*Now I have to pass the output of sql query values(a,b,c) I got above as an
input to Test.exe parameter 3 times in a for or while loop specific to batch
script one value at a time and executes the exe.
Example of values passed to exe looks as below.
Test.exe a -- 1st run in a loop
Test.exe b -- 2nd run in a loop
Test.exe c -- 3rd run in a loop
-- My code overview or assumption, I would need the below code snippet to be replaced
and rewritten in a way it executes successfully using batch script.*/
declare @name int
set @name = count(Name) from sys.tables
DECLARE @Counter INT
SET @Counter=1
WHILE ( @Counter <= @name)
BEGIN
C:\Users> .\Test.exe /Input_From_Sql_result_as_argument @Name
SET @Counter = @Counter + 1
END
我是批处理脚本的新手,如果您能对在批处理脚本中实现这一点提出一些想法,那将非常有帮助。如果我需要分享任何细节,请告诉我。
【问题讨论】:
-
如果您向我们展示批处理脚本,我会的。这是一个 .sql 脚本,不是批处理。
-
@GerhardBarnard 是的,最终输出和执行应该通过运行一个批处理文件来完成,但是批处理文件中的逻辑取决于从 SQL 表派生的结果集,并且应该作为输入传递给将根据记录数循环运行的exe作为sql表中的输出。
-
对于您示例的命令类型,您需要的批处理等效项是
forset和start。在 cmd.exe 中键入每个命令,后跟 /?对于语法
标签: sql-server powershell tsql batch-file exe