0

I am trying to find out with SQLEXPRESS is installed on a server by running the following

sqlcmd -S LT1183 -U uname -P pass123 -Q "SELECT @@servername"

If it is installed it gives out the server name but if its not, it throws the below error.

Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : TCP Provider: Error code 0x2AF9. Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.

I am looking for a way to suppress this error and instead show a custom error message. Is that possible?

Thanks in advance.

Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
oyeesh
  • 551
  • 9
  • 21

1 Answers1

0

This may not be possible with sqlcmd, since the error stems from connecting the the server, which upon error immediately terminates the sqlcmd.

if you can use powershell, you may find some luck using invoke-sqlcmd.

Powershell Try Catch invoke-sqlcmd

The Integrator
  • 2,068
  • 2
  • 11
  • 11
  • Got it. Unfortunately I cannot use Powershell. Is there an easy way to differentiate if `sqlcmd` is successful or not. All I want is to report the user that there is an issue connecting to SQLEXPRESS using shell script. – oyeesh Feb 15 '18 at 18:16