I'm making complete connection strings inside my procedure and would like to inject the name of the database server in them. Is there any way that I can detect the name from inside or am I doomed to passing it in?
views:
59answers:
2
+2
A:
So for MS SQL Server you could:
SELECT @@SERVERNAME
For MySQL, it's
SELECT variable_value as servername
FROM information_schema.global_variables
WHERE variable_name = 'hostname';
For Oracle, its:
SELECT global_name FROM global_name
Scott Anderson
2010-01-11 22:04:30
+4
A:
Use SERVERPROPERTY:
SERVERPROPERTY('MachineName')
: name of the SQL Server host name, cluster awareSERVERPROPERTY('ComputerNamePhysicalNetBIOS')
: name of physical machine name. In a cluster, is the name of the current active node. On a standalone instalation, is identical with MachineNameSERVERPROPERTY('InstanceName')
: name of the current SQL Server instance. NULL for default.
One thing I'd recomend against is the dreaded @@SERVERNAME. This property is notorious for getting out of sync with reality after a machine rename. I've seen way too many apps burned by this problem to place any trust on it. The correct rename procedure is in BOL, but few use it properly: How to: Rename a Computer that Hosts a Stand-Alone Instance of SQL Server.
Remus Rusanu
2010-01-11 22:17:32
Sweet! You're right @@SERVERNAME didn't give me the right value. Small follow up: if I want to append the domain name to the end of the server name would I have to loop through all the AD domains and pick the one I want or is there an easier way if there is only one?
Tomasz
2010-01-13 16:16:57
I actually had the very same problem recently and could not find a reasonable solution. I ended up adding a CLR function to the database that was returning the DNS suffix from the Network interface info, like http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipinterfaceproperties.dnssuffix(VS.85).aspx.
Remus Rusanu
2010-01-13 23:26:40