简体   繁体   中英

Command window through batch script is not stopping after execution

I have the below script in .bat file:

@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
  2. I have tried replacing pause with pause >nul but it did not work.

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

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