Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2002
    Location
    Sunnyvale, CA USA
    Posts
    78

    Unanswered: Windows batch to loop through all sql files and call sqlcmd

    I need to write a Windows batch file to loop through all sql files in a directory and call sqlcmd.exe (the SQL Server 2008 command line). Something like this:-

    (FOR %%i IN (*.sql) DO sqlcmd -S %1 -d %2 -E -i "%%i")

    But I want to drop out of the loop if any of the calls to sqlcmd fails. But nothing I try seems to work. Hope you can help. Thanks.

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool

    You could try something like this:
    Code:
    FOR %%i IN (*.sql) DO (
    sqlcmd -S %1 -d %2 -E -i "%%i"
    IF NOT ERRORLEVEL 0 GOTO :ERR
    )
    echo !Success. sqlcmd ok.
    GOTO :EOP
    :ERR
    echo !Error. sqlcmd failed.
    :EOP
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •