Question

I've come across an odd problem, which I'm sure is easily explained and fixed.

I'm using a .bat file to install a lot of programs, one of which is SQL server 2012 (if it matters, the other installations are just .net framework, OPOS drivers and POS software which uses SQL server).

I can get the batch file to silently instal everything, including SQL (with SSMS), which is great. But once SQL Server is installed I'd like to run a couple of SQL scripts to create/attach databases.

I know that the install works (if I dont' try to doanything other than the installs), and I know that the SQLCMD works (if I run it seperately, after the install), but if I try to run the SQLCMD after the install, in the same batch file it fails with the standard 'SQLCMD is not recognised as an internal or external command...'

I've put this down to I need to restart CMD to get it to recoginise the new command (i.e. the SQLCMD), so I figured I'd split out the SQLCMD commands into a separate batch file and call it, but it still doesn't work. I have to physically close my original batch file down before CMD.exe picks up the new commands.

So... is it possible to 'refresh' cmd.exe so that the newly installed SQLCMD commands are useable from the original batch file??

Here is (some of) my script. (note that I've removed all of the other install before SQL Server)

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

"%~dp0SQLServer.exe" /QS /INDICATEPROGRESS /ACTION=install /FEATURES=SQL,SSMS /INSTANCENAME=Datasym /SECURITYMODE=SQL /SAPWD=Welcome21ST /IACCEPTSQLSERVERLICENSETERMS
echo.
echo SQL Server installed

sqlcmd -S %computername%\DATASYM -U sa -P Welcome21ST -i %script%

The %script% variable changes (like I said, there are multiple scripts I'd like to run). And I know that the scripts themselves work.

Why does CMD not recognise SQLCMD as a command??

I tried to replace the line:

sqlcmd -S %computername%\DATASYM -U sa -P Welcome21ST -i %script%

with

call sqlscript.bat

as I thought that would open another CMD,exe (which it does), so I assumed that the second CMD would see the ew command (SQLCMD), but it doesn't seem to .

Any help on how I can get the SQLCMD to work in the original batch file would be greatly appreciated.

Was it helpful?

Solution

As you've already found, the current CMD window won't pick up the new changes to the system PATH environment variable. If for some reason you don't want to put the full path to sqlcmd (which I think is the best solution), you might try doing this instead.

start /wait cmd /c sqlcmd -S %computername%\DATASYM -U sa -P Welcome21ST -i %script%

This will spawn a new window and wait for the sqlcmd call to finish before returning to your original script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top