Unable to run sqlcmd using batch

  • Hi All

    I use the following batch to run list of scripts in folder

    @echo off

    set scripts=%~dp0\_scripts.txt

    set Instance=%1

    set DataBase=%2

    set Password=%3

    set userName=%4

    if "%instance%" equ "" set instance=-".\MSSQL"

    if "%DataBase%" equ "" set DataBase=-"Northwind"

    if "%Password%" equ "" set Password=-"123"

    if "%userName%" equ "" set userName=-"sa"

    rem

    rem Make sure the scripts file exists.

    rem

    if not exist %scripts% (

    echo.

    echo Scripts file "%scripts%" does not exist.

    echo.

    goto error

    )

    rem

    rem Clean output of any previous runs

    rem

    if exist %~dp0\__tmp* del /q %~dp0\__tmp*

    rem

    rem Apply all SQL scripts in order as listed in _scripts.txt

    rem

    rem NOTE: if any of the script filenames contain a space, this

    rem for loop will only see the 1st component of the filename,

    rem and then fail (file not found). Do not use spaces in the

    rem script filename.

    rem

    for /f "delims==" %%x in (%scripts%) do (

    echo.

    echo Applying script %%x...

    if not exist %CD%\%%x (

    echo.

    echo ERROR: script does not exist '%CD%\%%x'

    echo.

    goto error %%x

    )

    rem

    rem Make sure script exists

    rem

    if not exist "%~dp0\%%x" (

    echo.

    echo ERROR: script does not exist '%~dp0\%%x'

    echo.

    goto error

    )

    rem

    rem Run the script, show the output on stdout and tee this to a

    rem file with the same name as the script, prefixed with __tmp_

    rem

    sqlcmd -S%instance% -d%DataBase% -u%userName% -p%Password% -i"%~dp0\%%x" -o"%~dp0\__tmp_%%x"

    )

    echo.

    echo Done!

    goto end

    :error

    echo.

    endlocal

    exit /b 1

    :end

    endlocal

    exit /b 0

    it gives error Sqlcmd: '-sa': Unexpected argument. Enter '-?' for help.

    any idea how to fix that

  • I don't see -sa in your sqlcmd call. Is %userName% "-sa" or is it "sa"

  • You have a stray '-' on line 11 (ish) of your script where you set the userName var

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply