Issue with SQL install and sqlcmd in same .bat file

10,542

I'm afraid I cannot be 100% sure on this issue and I cannot add it to a comment yet so I have to put it here.

1.Does anyone know why this is happening?

  • I'd Imagine this is happening because when you install SQL Server it will add specific paths to the Windows Environment Variables. As the batch script is used to install the SQL Server edition I would guess that it is not refreshing the Windows Environment Variables, meaning that the SQLCMD.exe file contained within C:\Program Files\Microsoft SQL Server\100\Tools\Binn\ (Or whereever you are installing it is not cached).

2.Is there a solution that involves launching a single .bat file only?

  • If you are installing it to a default location each time, for instance the C:\Program Files\Microsoft SQL Server. Then you might be able to just launch the SQLCMD straight from there. I.E.

C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.exe -q etc....

You might be worth creating a variable to that path to make it easier.

I don't know of a process from a batch script to refresh or look for new Windows Environment Variables, however that i'd imagine is the likely cause.

Share:
10,542
wonderman
Author by

wonderman

Updated on July 24, 2022

Comments

  • wonderman
    wonderman almost 2 years

    I am trying to use a single .bat file to: 1) install SQL Server 2008 R2 Express from SQLEXPR_x64_ENU.exe on Windows 7 Professional 64-bit machines, and 2) run some .sql scripts once the install is complete.

    It appears that if sqlcmd is run from the same .bat file that ran the install, then sqlcmd fails with the following error: "'sqlcmd' is not recognized as an internal or external command, operable program or batch file."

    If the install and .sql scripts are run from separate .bat files, then I have no issues (and so I know that the problem is not with the sqlcmd syntax). I have tried using a single .bat and running the sqlcmd commands in a new window (start cmd /c "sqlcmd ...") and calling a second .bat file, containing the sqlcmd commands, from within the original .bat file (\filepath\file.bat) but the same error occurs in each case.

    I don't want the user to have to launch more than one .bat file because that opens up problems of running the .bat files in the wrong order, running one but not the other, etc.

    My two questions are:

    1. Does anyone know why this is happening?
    2. Is there a solution that involves launching a single .bat file only?