3

I am trying to apply multiple SQL scripts to an Ingres database (using a vnode setup). The testing phase will require it done to four databases. Trying to nip this annoyance in the bud I've started a batch file but receive the error above. Many of the solutions found suggest that the batch file will evaluate everything within a block when it starts but I cannot see the forest for the trees. I have a suspicion that the parts in :1ST and :2ND are causing the problems but they need to be done. SQL, Batch and command window output are below


UPDATE core SET sysflag='O'

@ECHO off
SET VN1=dave
SET DB1=dbtest1
SET DB2=dbtest2
SET SQL1=open.sql
SET SQL2=open.sql

:MENU
CLS
ECHO 1 - Leave
ECHO 2 - Database1
ECHO 3 - Database2

SET /P M=Choose then press ENTER:
IF "%M%"=="1" GOTO EOF
IF "%M%"=="2" GOTO 1ST
IF "%M%"=="3" GOTO 2ND
GOTO MENU

:1ST
SET DATABASE=%VN1%::%DB1%
GOTO RUNSQL
:2ND
SET DATABASE=%VN1%::%DB2%
GOTO RUNSQL

:RUNSQL
ECHO Applying SQLs to %DATABASE%
SQL %DATABASE% < %SQL1% > log_%SQL1%.txt
PAUSE
SQL %DATABASE% < %SQL2% > log_%SQL2%.txt
PAUSE
GOTO MENU

:EOF

C:\Users\me\BUILD>IF UPDATE core SET sysflag='O'==1 GOTO EOF
4

1 回答 1

1

您期望 的值为%M%1、2 或 3。但不知何故,该值为UPDATE core。IF 语句失败,因为左值中间有一个空格。必须对空格等标记分隔符进行转义,或者应引用每一侧的整个字符串。您可以更改您的语句以IF "%M%"=="1" GOTO EOF消除错误,但它仍然不会给出您想要的结果。

SET /P 语句从标准输入读取值。我假设您没有输入 value UPDATE core,而是您的输入被重定向或管道传输。您正在为脚本提供错误的值。

您应该添加错误处理,以便在输入不是 1、2 或 3 时代码不会落入 :1ST。


您可以为您的 SET /P 语句将输入显式重定向到控制台。这样,它将忽略为批处理脚本提供的重定向输入或管道输入。

<con: SET /P "M=Choose then press ENTER: "

但是你的整个设计似乎有问题。如果您正在管道或重定向脚本的输入,那么在循环中呈现交互式选项菜单是没有意义的。如果用户从不按 1 退出会发生什么?最终管道或重定向的输入将被耗尽,然后你就会遇到问题。

于 2012-08-29T12:36:01.463 回答