I know this is very similar to this SO question, but I've modified the script to do what I'm trying to do and when I run the batch file it just closes right after hitting Enter after entering the company ID.
I need to check that the company id the user enters when the batch script runs is valid. I'm using user-entered values elsewhere in the script, so I'm sure that's correct, but I'm not getting why the script doesn't run as-is.
Here's my script:
@SETLOCAL ENABLEDELAYEDEXPANSION
@ECHO OFF
ECHO Enter the Company ID the data is for
SET /p _COMPANYID=Company ID:
SET _INSTANCE=someinstance
SET _DATABASE=somedb
for /f %%a in (
sqlcmd -b -S %_INSTANCE% -d %_DATABASE% -E -l 2 -Q "SET NOCOUNT ON; select count(*) from glb_companies where companyid = %_COMPANYID%"
)
do set _RESULT=%%a
echo %_RESULT%
@pause
I've tried the sqlcmd line with and without single quotes. The switches I have in the sqlcmd are what I'm using elsewhere and I've also tried it just with the switches from the other SO post. Nothing's worked for me yet.