SQLCMD does not execute command after silent installation of SQL Server with Inno Setup

Asked

Viewed 205 times

0

I have a script from Inno Setup which provides the user with the installation of SQL Server 2012, and upon completion, he should restore a database in a specified path, in order to 'componentize' my installer, put as option to restore the database, and when checked this option, the installer will run a file .bat, that will execute a command SQL via sqlcmd and restore the database through a file .Bak

My problem is that the database restore does not occur when requested in the installer, and I could not find the reason for it.

Follows script from Inno Setup:

[Files]
;SQL EXPRESS
Source: "Setup\SQL Server 2012 Express\SQL2012EXPR_x86.exe"; DestDir: "{app}"; Flags: 
deleteafterinstall onlyifdoesntexist; Components: PDVM8Servidor

Source: "Setup\SQL Server 2012 Express\SQL2012EXPR_x64.exe"; DestDir: "{app}"; Flags: 
deleteafterinstall onlyifdoesntexist; Components: PDVM8Servidor

;Banco de dados
Source: "Dependencias\Banco de dados\PDVM8.bak"; DestDir: "{sd}\PDVM8"; Components: 
PDVM8Servidor/BancoDeDadosVazio

Source: "Dependencias\Banco de dados\sql-pdvm8.bat"; DestDir: "{sd}\PDVM8"; Components: 
PDVM8Servidor/BancoDeDadosVazio


[Run]

;SQL EXPRESS
Filename: "{app}\SQL2012EXPR_x86.exe"; Parameters: "/QS /INSTANCENAME=MSSQLSERVER 
/IACCEPTSQLSERVERLICENSETERMS /INDICATEPROGRESS /ACTION=Install /FEATURES=SQL,AS,RS,IS,Tools 
/AGTSVCSTARTUPTYPE=Automatic /SQLSVCACCOUNT=""NT AUTHORITY\SYSTEM"" 
/SQLSYSADMINACCOUNTS=builtin\administrators /ADDCURRENTUSERASSQLADMIN=true /TCPENABLED=1 /NPENABLED=1 
/SECURITYMODE=SQL /UpdateEnabled=false /SAPWD=IWTI3K"; WorkingDir: "{app}"; Flags: runasoriginaluser 
shellexec waituntilterminated; StatusMsg: "Instalando SQL Server 2012 Express 32-bits..."; Check: 
"not IsWin64"; MinVersion: 0,6.1; Components: PDVM8Servidor;


Filename: "{app}\SQL2012EXPR_x64.exe"; Parameters: "/QS /INSTANCENAME=MSSQLSERVER 
/IACCEPTSQLSERVERLICENSETERMS /INDICATEPROGRESS /ACTION=Install /FEATURES=SQL,AS,RS,IS,Tools 
/AGTSVCSTARTUPTYPE=Automatic /SQLSVCACCOUNT=""NT AUTHORITY\SYSTEM"" 
/SQLSYSADMINACCOUNTS=builtin\administrators /ADDCURRENTUSERASSQLADMIN=true /TCPENABLED=1 /NPENABLED=1 
/SECURITYMODE=SQL /UpdateEnabled=false /SAPWD=IWTI3K"; WorkingDir: "{app}"; Flags: runasoriginaluser 
shellexec waituntilterminated; StatusMsg: "Instalando SQL Server 2012 Express 64-Bits..."; Check: 
IsWin64;       MinVersion: 0,6.1; Components: PDVM8Servidor;  

Filename: "{sd}\PDVM8\sql-pdvm8.bat"; Parameters: "install";  Flags: runasoriginaluser shellexec 
waituntilterminated; MinVersion: 0,6.1; Components: PDVM8Servidor/BancoDeDadosVazio;


[Dirs]
Name: "{sd}\PDVM8"; permissions: everyone-modify admins-full; Components: PDVM8Servidor

Follows script sql-pdvm8.bat that restores the database:

    @echo *******************************************************
    @echo. 
    @echo *** Restaurando banco de dados vazio... ***
    @echo.
    @echo *******************************************************

    timeout /t 10 /nobreak

    sqlcmd -E -S . -Q "RESTORE DATABASE [PDVM8] FROM DISK='C:\PDVM8\PDVM8.bak' WITH RECOVERY"

    exit

    Pause
    EXIT /B

Considerations:

  • If I run the file . bat directly in the folder, works
  • If I run the installer, install SQL server, and run the installer for the second time, works, the bank is restored.
  • If I run the installer, cancel the installation of SQL Server, and force the execution of the second step, which would restore the database, works, the bank is restored.
  • If I perform any other function on sqlcmd other than this, but in the same way (after installing SQL Server, make a call to a file . bat, which executes a function via sqlcmd), works
  • I tried to run a trial with the tag Afterinstall of the Inno, but the bank also does not restore.
  • Analyzing I see that the file . bat is actually called and opened, the restore command runs, but nothing happens, the database is not restored.
  • I didn’t put the code 100% complete Inno Setup, because it is very extensive. All that concerns the problem is in the posting of the above code.

The code only does not work when it is a first installation of SQL Server, and following the restoration of the database, and I see everything is ok. Thank you from now on for any hint that can help me solve this.

1 answer

1


The problem was in the execution of the file .bat. With help from the community, I took a look at this question https://stackoverflow.com/questions/37324386/debugging-non-working-batch-file-or-command-executed-from-inno-setup-installer and got "Debuggar" the file execution. During the run, the SQLCMD command was not recognized, and so the bank was not restored. To resolve, I needed to make modifications to the Inno Setup and also the archive .bat.

In the Inno script, the Component responsible for executing the database restore was thus:

Filename: "{cmd}"; Parameters: "/C ""{sd}\PDVM8\sql-pdvm8.bat"" install"; Flags: runasoriginaluser shellexec waituntilterminated; MinVersion: 0,6.1; Components: PDVM8Servidor;

And the file .bat:

@echo *******************************************************
@echo. 
@echo *** Restaurando banco de dados vazio... ***
@echo.
@echo *******************************************************

cd C:\Program Files\Microsoft SQL Server\110\Tools\Binn

sqlcmd -E -S . -Q "RESTORE DATABASE [PDVM8] FROM DISK='C:\PDVM8\PDVM8.bak' WITH 
RECOVERY"

EXIT /B

Ultimately, the biggest problem was that I should run the command within the relative path where SQLCMD.exe gets installed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.