Search code examples
nsissqlcmd

Execute SQLcmd and then return query result as variable to NSIS


I am creating an NSIS installer and I need to get the version of SQL server the users has installed.

The user will give me the instance name so I can connect to the box. Then I run this sql query select SERVERPROPERTY ('ProductMajorVersion') and get the major version number.

But when I try to do is from NSIS I am not succeeding as it errors when it is executing the query so I do not get the result from the console window.

Below is what I have in my NSIS file:

nsExec::ExecToStack "'sqlcmd -S $SQL_Instance_Name -E -h-1 -Q $\"SET NOCOUNT ON; select SERVERPROPERTY ($\'ProductMajorVersion$\')$\"'"

    Pop $0
    Pop $1

I have also tried:

ExecCmd::Exec  "'sqlcmd -S $SQL_Instance_Name -E -h-1 -Q $\"SET NOCOUNT ON; 
select SERVERPROPERTY ($\'ProductMajorVersion$\')$\"'"

    Pop $0
    Pop $1

But I am just not getting my desired result any advise or suggestions would be appreciated.

Alternatively if there in easier and quicker way to get this information from the Registry then that would be fine as well.

TIA Andy


Solution

  • The application should be specified with a full path and it needs to be quoted with a double quotes:

    To ensure that command are executed without problems on all windows versions, is recommended to use the following syntax:

    nsExec::ExecToStack [OPTIONS] '"PATH" param1 param2 paramN'

    Section
    StrCpy $0 "-S $SQL_Instance_Name ....." ; Putting the parameters here makes them easier to debug etc
    MessageBox mb_ok $0 ; Use this to make sure the parameters look correct
    nsExec::ExecToStack '"c:\path\to\sqlcmd.exe" $0'
    Pop $0
    Pop $1
    DetailPrint $0,$1 ; $0 will be "error" if nsExec cannot start the application
    SectionEnd