In an interactive batch script, using sqlcmd, I need to query a table for values from a single column that I know will return multiple records. The return values are just integers, but I need to display them so the user can specify which value should be used later on in the batch script.
Here's what I have right now:
@SETLOCAL ENABLEDELAYEDEXPANSION
@ECHO OFF
SET _INSTANCE=<someinstance<
SET _DATABASE=<somedb>
:getcompanyids
REM Query glb_Companies for available IDs
ECHO.
ECHO Getting Company IDs ...
ECHO.
ECHO.
FOR /F %%i IN ( 'sqlcmd -b -S %_INSTANCE% -d %_DATABASE% -E -l 2 -Q "SET NOCOUNT ON; SELECT CompanyID FROM glb_Companies"'
) DO (
SET IDs=%%i
)
ECHO Company IDs: %IDs%
ECHO.
ECHO.
ECHO After If Not Defined
@pause
As is, only one line is displayed - for the first record found:
Company IDs: 1
In the particular instance and db I am testing this on, I know I should get the values 1, 2, and 13. How do I display those? They can be on the same line like such:
1,2,13
or on separate lines
1
2
13
It doesn't really matter, so long as I can display in a readable fashion all of the values returned from the query.
My searching has only turned up PowerShell scripts, so I'm not finding much help that way.
I'm guessing I either have to get the returned values into an array that I can iterate through (if that can be done in sqlcmd) or somehow iterate through the result set directly.