简体   繁体   English

通过批处理脚本的命令窗口在执行后没有停止

[英]Command window through batch script is not stopping after execution

I have the below script in .bat file:我在 .bat 文件中有以下脚本:

@echo off

setlocal enableextensions enabledelayedexpansion

for /f %%a in ('sqlcmd -S RCOVSVR3742 -d FRK -E -Q "SET NOCOUNT ON;SELECT count(*) FROM [FRK].[dbo].[interface_log] where interfaceid = '1F88027F-44A0-4089-B6C2-129E9206E478' and cast(idata_stamp as date) = dateadd(dd,-2,cast(getdate() as date))
and idata is not null"')
do set ColumnVar=%%a

echo %ColumnVar%

pause

When i run the above code through batch script , the command window just doesnt stop.当我通过批处理脚本运行上述代码时,命令窗口不会停止。

Options Tried:尝试的选项:

  1. I have tried replacing pause with cmd /k and still it did not work我试过用 cmd /k 替换 pause ,但它仍然不起作用
  2. I have tried replacing pause with pause >nul but it did not work.我试过用 pause >nul 替换 pause ,但它没有用。

I am running the script from the server mentioned in the script.我正在从脚本中提到的服务器运行脚本。

Can anyone please help me to achieve this?任何人都可以帮助我实现这一目标吗?

As a supplement to my comments, this is what I believe your supplied code should have looked like:作为对我的评论的补充,我认为您提供的代码应该如下所示:

@Echo Off
For /F %%a In (
    'sqlcmd.exe -S RCOVSVR3742 -d FRK -E -Q "SET NOCOUNT ON;SELECT count(*) FROM [FRK].[dbo].[interface_log] where interfaceid = '1F88027F-44A0-4089-B6C2-129E9206E478' and cast(idata_stamp as date) = dateadd(dd,-2,cast(getdate() as date)) and idata is not null"'
) Do Set "ColumnVar=%%a"
Echo(%ColumnVar%
Pause

Please try the above in the first instance, to ensure that the line breaks, basic batch file syntax or the name of your batch file isn't the issue.请首先尝试上述操作,以确保换行符、基本批处理文件语法或批处理文件的名称不是问题。

I suspect that you may have named your script, sqlcmd.bat or sqlcmd.cmd .我怀疑您可能将脚本命名为sqlcmd.batsqlcmd.cmd If you have, the code above will fix your issue.如果有,上面的代码将解决您的问题。 Although I'd still recommend that you also rename it to something that isn't already an executable file name.尽管我仍然建议您将其重命名为还不是可执行文件名的名称。

尝试在批处理文件的末尾添加exit命令。

You can remove the line:您可以删除该行:

setlocal enabledelayedexpansion enableextensions

And add at the end:并在最后添加:

exit /b An error code-Optional

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

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