user801116
user801116

Reputation:

Why does my if Else if statement not work in Batch script?

My Script

echo "Enter your choice (1 or 2 or 3) :"
set /p dbchoice=

IF %dbchoice EQU 1 ( 
    set dbtype="oracle"
) ELSE ( 
    IF %dbchoice EQU 2 ( 
        set dbtype="sqlserver" 
    )
) ELSE ( 
    IF %dbchoice EQU 3 ( 
        set dbtype="db2" 
    )
) ELSE (
    echo "Incorrect choice" 
)

I get the following output:

E:\csmilm>set /p dbchoice=
1
ELSE was unexpected at this time.
E:\csmilm>) ELSE (
E:\csmilm>

What is the problem here?

Upvotes: 5

Views: 34436

Answers (1)

ChrisBD
ChrisBD

Reputation: 9209

Closing brackets in the wrong place. Try:

IF %dbchoice EQU 1 ( 
    set dbtype="oracle"
) ELSE ( 
    IF %dbchoice EQU 2 ( 
        set dbtype="sqlserver" 
    ) ELSE ( 
        IF %dbchoice EQU 3 ( 
            set dbtype="db2" 
        ) ELSE (
            echo "Incorrect choice" 
        )
    )
)

Upvotes: 11

Related Questions