我正在尝试从bat文件中使用的sqlcmd查询返回单个值。 似乎我的sqlcmd查询中使用的语法有一些错误。我对cmd编码很新,任何指导都会有所帮助。我在服务器上使用此查询。
@ECHO off
echo _________________________________________________________
Echo The following inputs are required:
Echo [1] Database Server Name
Echo [2] Configuration Type
echo ---------------------------------------------------------
CD %~dp0
SET /p DBSERVER="Enter [1]: "
SET /p CFIGTYPE="Enter [2]: "
echo Deploying...
ECHO %CFIGTYPE%
::-----------------------------------------------TEST1----------------------------------------
IF (%CFIGTYPE% == 'TEST1' ){
for /f %%a in ('sqlcmd -S %DBServer% -v DBServer="%DBServer%" -Q "SET NOCOUNT ON; select ConfigValue FROM SERVICE.dbo.configuration WHERE ConfigOption = 'TEST1' "')
do set ColumnVar=%%a
echo %ColumnVar%
CMD \K
IF (%ColumnVar% == 'Y'){
Echo WOHOOOO
cmd /k
}
Else{
ECHO NOOOOOO
CMD /K
}
我的代码在到达sqlcmd部分后才会关闭。
根据评论修改代码:
(不解决问题)
@ECHO off
echo _________________________________________________________
Echo The following inputs are required:
Echo [1] Database Server Name
Echo [2] Configuration Type
echo ---------------------------------------------------------
SET /p DBSERVER="Enter [1]: "
SET /p CFIGTYPE="Enter [2]: "
echo Deploying...
ECHO %CFIGTYPE%
::-----------------------------------------------TEST1----------------------------------------
IF "%CFIGTYPE%"=="TEST1" (
for /f %%a in ('sqlcmd -S %DBServer% -v DBServer="%DBServer%" -Q "SET NOCOUNT ON; select ConfigValue FROM SERVICE.dbo.configuration WHERE ConfigOption = 'TEST1' "')
do set ColumnVar=%%a
echo %ColumnVar%
IF "%ColumnVar%"=="Y" (
Echo WOHOOOO
cmd /k
)
Else (
ECHO NOOOOOO
CMD /K
)
)
新版本:
@ECHO off
echo _________________________________________________________
Echo The following inputs are required:
Echo [1] Database Server Name
Echo [2] Configuration Type
echo ---------------------------------------------------------
SET /p DBSERVER="Enter [1]: "
SET /p CFIGTYPE="Enter [2]: "
echo Deploying...
ECHO %CFIGTYPE%
::-----------------------------------------------TEST1----------------------------------------
IF "%CFIGTYPE%"=="TEST1" (
for /f %%a in ('sqlcmd -S %DBServer% -v DBServer="%DBServer%" -Q "SET NOCOUNT ON; select ConfigValue FROM SERVICE.dbo.configuration WHERE ConfigOption = 'TEST1' "')
IF "%%a"=="Y" (
Echo WOHOOOO
PAUSE
) Else (
ECHO NOOOOOO
PAUSE
)
)
答案 0 :(得分:0)
我没有设法找到一个允许我从sql查询中为变量设置值的解决方案。相反,我已将结果输出到文件并从那里读取结果,然后删除该文件。这是代码:
@ECHO off
echo _________________________________________________________
Echo The following inputs are required:
Echo [1] Myserver
echo ---------------------------------------------------------
CD %~dp0
SET /p DBSERVER="Enter [1]: "
:: Get Configuration Type
sqlcmd -S %DBServer% -E -i test.sql -b -v TYPE="CFIGTYPE" -h -1 >result.txt
set /p CFIGTYPE=<result.txt
call :Trimspaces CFIGTYPE %CFIGTYPE%
del "result.txt"
:: ------------------------------------------------ FUNCTIONS--------------------------------------------------------------------------------------------------------------
:trimSpaces2 retval string -- trims spaces around string and assigns result to variable
:: -- retvar [out] variable name to store the result in
:: -- string [in] string to trim, must not be in quotes
for /f "tokens=1*" %%A in ("%*") do set "%%A=%%B"
EXIT /b
:trimSpaces varref -- trims spaces around string variable
:: -- varref [in,out] - variable to be trimmed
call call:trimSpaces2 %~1 %%%~1%%
EXIT /b
由于输出文件将包含空,我已经包含了一个取自的函数 https://www.dostips.com/DtTipsStringOperations.php
这会在读取文件时消除空白区域。
我有一个额外的test.sql,其中包含以下代码:
SET NOCOUNT ON;
select configvalue FROM Service.dbo.Configuration WHERE ConfigOption = '$(type)'