I'm looking for a way to identify a particular SQL Server Instance from a stored procedure within the server. What I need is to be sure that my app is connecting to that particular instance and not to some other instance with the same name/ip address.
4 Answers
I'd take a look at the SERVERPROPERTY function - you could use a mixture of InstanceName, MachineName, ServerName or ProcessID for example.
Comments
You should look at using the @@SERVICENAME function.
Here's the description from the @@SERVICENAME MSDN article
Returns the name of the registry key under which SQL Server is running. @@SERVICENAME returns 'MSSQLSERVER' if the current instance is the default instance; this function returns the instance name if the current instance is a named instance.
The @@SERVERNAME will return the name of the server the database is running on, which may also be helpful to you.
Usage example: SELECT @@SERVERNAME as 'Server Name', @@SERVICENAME AS 'Service Name'