Yuval Perelman
Yuval Perelman

Reputation: 4809

unable to connect to default instance using it's full name

I am developing an app that needs to get from a user a server name or address and a db name and build a folder structure based on that. The problem is that in order to have the same folder for all the different ways there are to get the path to the instance (localhost, ip address, etc.) i'm running the following Query:

select cast(SERVERPROPERTY('MachineName')as varchar)+'\'+@@servicename

on the target server and set my folder structure based on that, and in that format saves the connection that the user gave (doesn't matter if i got ip or a server name, there is one connection string for all).
My problem is that when the instance is the default instance on the target machine, I cant seem to connect using MACHINE_NAME\MSSQLSERVER. I can only log in using the machine without instance name. So I need to either find a way to connect to the instance using its full name (preferred) or to find a way figure out if the targeted instance is the default one.
Any help would be very appreciated.

Upvotes: 0

Views: 455

Answers (1)

Wendy
Wendy

Reputation: 660

'MSSQLSERVER' is reserved for default instance, so you can

SELECT CAST(SERVERPROPERTY('MachineName')AS VARCHAR)+ CASE WHEN CHARINDEX('MSSQLSERVER', @@SERVICENAME, 1) >0 THEN '' ELSE '\'+@@SERVICENAME END

Upvotes: 1

Related Questions