none
Sqlcmd not working.

    Question

  • Hi,

    I have created a batch file to create a database. There are two parameters that i pass to the batch file, 1)server name 2)Path to create the .mdf/.ldf files. When i open command prompt I am able to access sqlcmd, but the moment i change the directory in command prompt, i get a error message 'sqlcmd' is not a recognized internal or external command. I havent used sqlcmd before. Also, when i run the batch file providing the parameters I get the same error message "sqlcmd" is not recognized. Am i missing something obvious? Any help is much appreciated.


    MSBI Developer

    Wednesday, April 11, 2012 4:50 PM

Answers

  • Hi,

    Thanks for the inputs. I could find where the problem exactly is, but i dont know how to solve it. My batch file lokks like below:

    cls

     @set /p server= enter the server name : local

     @set /p path= enter the path  :"C:\Program Files"

     @echo.* Parameters specified
      @echo.*   Server Name                              : %server%

      @echo.*   Path                                           : %path%

    sqlcmd -S %1   -i createdb.sql -v path=%2

    I get the "Sqlcmd not recognized" error when i execute the above batch script. If I remove the "-v path =%2", then it executes. Is this an incorrect way of passing parameters to a .sql script? Also, how to pass two parameters to a single sql script?.


    MSBI Developer

    why do you override the path environment variable? any why do you need that path within the scripts passed to sqlcmd?

    additionally you should call setlocal at the beginning and endlocal at the end of the batch to restore the environment setting.

    • Marked as answer by TDPN Thursday, April 12, 2012 12:37 PM
    Thursday, April 12, 2012 12:01 PM

All replies

  • is sqlcmd set in your path environment variables, i.e. echo %path%


    Thanks,

    Andrew Bainbridge
    SQL Server DBA

    Please click "Propose As Answer" if a post solves your problem, or "Vote As Helpful" if a post has been useful to you

    Wednesday, April 11, 2012 4:56 PM
  • Hi,

    Yes, the environment variable Path has got "C:\Program files\Microsoft sql Server\Tools\Bin" as one of the values. 


    MSBI Developer

    Wednesday, April 11, 2012 5:10 PM
  • are you opening a command shell ("Command Prompt") or have you started a Powershell command ?

    if you don't know the difference, what looks the command line prompt like ?

    if you can execute sqlcmd from the initial directory but as soon you're changing the directory it will no longer found but the directory is listed in the path than something is wrong.

    Are you using a 64-bit environment and could you check with Windows Explorer (search function) if you've several versions of sqlcmd on your system installed.

    Could you try to add the initial directory to the path environment variable again - at the beginning - and check it again if it works when changing to a different directory?

    Wednesday, April 11, 2012 6:44 PM
  • Hi,

    Thanks for the inputs. I could find where the problem exactly is, but i dont know how to solve it. My batch file lokks like below:

    cls

     @set /p server= enter the server name : local

     @set /p path= enter the path  :"C:\Program Files"

     @echo.* Parameters specified
      @echo.*   Server Name                              : %server%

      @echo.*   Path                                           : %path%

    sqlcmd -S %1   -i createdb.sql -v path=%2

    I get the "Sqlcmd not recognized" error when i execute the above batch script. If I remove the "-v path =%2", then it executes. Is this an incorrect way of passing parameters to a .sql script? Also, how to pass two parameters to a single sql script?.


    MSBI Developer

    Thursday, April 12, 2012 6:01 AM
  • Hi,

    Thanks for the inputs. I could find where the problem exactly is, but i dont know how to solve it. My batch file lokks like below:

    cls

     @set /p server= enter the server name : local

     @set /p path= enter the path  :"C:\Program Files"

     @echo.* Parameters specified
      @echo.*   Server Name                              : %server%

      @echo.*   Path                                           : %path%

    sqlcmd -S %1   -i createdb.sql -v path=%2

    I get the "Sqlcmd not recognized" error when i execute the above batch script. If I remove the "-v path =%2", then it executes. Is this an incorrect way of passing parameters to a .sql script? Also, how to pass two parameters to a single sql script?.


    MSBI Developer

    why do you override the path environment variable? any why do you need that path within the scripts passed to sqlcmd?

    additionally you should call setlocal at the beginning and endlocal at the end of the batch to restore the environment setting.

    • Marked as answer by TDPN Thursday, April 12, 2012 12:37 PM
    Thursday, April 12, 2012 12:01 PM
  • Hi,

    You are a life saver. I am a newbie in batch file programming. so, i dont know that "path" refers to environment variable. I wanted to use "Path" variable to set the location to create .mdf file of the database. I changed it now.


    MSBI Developer

    Thursday, April 12, 2012 12:40 PM