0

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.

marky
  • 4,878
  • 17
  • 59
  • 103
  • 1
    The ´do` has to be on the same line as the closing parentheses. If you run the batch in an open cmd window with `echo on` you have the chance to see any error messages. –  Jan 15 '19 at 22:08
  • That fixed it - it's always the simple things that break stuff! Thanks, @LotPings – marky Jan 17 '19 at 16:37

0 Answers0