批处理文件的摘录
:: This setlocal is done to use variable syntax '!varname!' in nested for loop
setlocal enableextensions enabledelayedexpansion
echo on
SET AUTOMATION_ACCOUNT_ID = some value
SET DB_SERVER_NAME = some value
SET DB_NAME = some value
SET all remaining variables
::AUTOMATION_ACCOUNT_CHECK
:: Checking if Automation Account is already created on DB (If not then create it first & then move to FREE_TRAIL_ACCOUNT_CHECK process)*
FOR /F "usebackq tokens=1" %%i IN (`sqlcmd -S %DB_SERVER_NAME% -h -1 -d %DB_NAME% -U %DB_SERVER_USERNAME% -P %DB_SERVER_PASSWORD% -Q"set nocount on; select (count(*)) from dbo.[ACCOUNTS] where ACCOUNT_ID ='%AUTOMATION_ACCOUNT_ID%';"`) DO set ACCOUNTCOUNT=%%i
echo "ACCOUNT COUNT is "!ACCOUNTCOUNT!
if !ACCOUNTCOUNT!==0 goto CREATE_AUTOMATION_ACCOUNT:
goto FREE_TRAIL_ACCOUNT_CHECK:`
我想在oracle实例上执行上面的脚本。
通过谷歌搜索我发现sqlcmd不能用于oracle DB,我必须使用sqlplus。使用sqlplus,我将以上脚本转换为如下
::AUTOMATION_ACCOUNT_CHECK
:: Checking if Automation Account is already created on DB (If not then create it first & then move to FREE_TRAIL_ACCOUNT_CHECK process)
FOR /F "usebackq tokens=1" %%i IN ('sqlplus
%DB_SERVER_USERNAME%/%DB_SERVER_PASSWORD%@%DB_SERVER_NAME% @sqlscript.sql') DO @set ACCOUNTCOUNT=%%i
echo "ACCOUNT COUNT is "!ACCOUNTCOUNT!
其中sqlscript.sql包含
SELECT COUNT(*) from ACCOUNTS;
exit;
但是这不起作用,因为我在尝试执行此批处理文件时在命令提示符下获得响应
C:\Setup>FOR /F "usebackq tokens=1" %i IN ('sqlplus uname/pwd@host:port @sqlscript.sql') DO @set ACCOUNTCOUNT=%i
C:\Setup>echo "ACCOUNT COUNT is "!ACCOUNTCOUNT!
"ACCOUNT COUNT is "sqlplus
C:\Setup>exit /B 0
我正在打印'sqlplus'而不是帐户计数... 你能帮助我解决这个问题吗?
答案 0 :(得分:1)
在FOR循环选项中使用usebackq
意味着您应该在需要执行命令时使用后引号,否则将其用作字符串。
FOR /F "usebackq tokens=1" %%i IN (`sqlplus ...
代码开头的SET
语句无法按预期工作。
SET DB_NAME = some value
将创建一个名为DB_NAME<space>
的变量,因此无法使用%DB_NAME%
访问该变量。
使用SET
答案 1 :(得分:0)
您遇到的主要问题是您指定的是userbackq
,但您已经在sqlplus
电话周围使用了正常的直接引号。您还需要添加-s
(静默)标志以禁止SQL * Plus标题:
FOR /F "usebackq tokens=1" %%i IN (
`sqlplus -s %DB_SERVER_USERNAME%/%DB_SERVER_PASSWORD%@%DB_SERVER_NAME% @sqlscript.sql`
) DO @set ACCOUNTCOUNT=%%i
通过这些更改,您会看到类似的内容:
"ACCOUNT COUNT is "53
所以你可能也希望从echo命令中删除双引号(并用@
隐藏命令,但是我假设你现在正在显示它们以进行调试):
@echo ACCOUNT COUNT is !ACCOUNTCOUNT!