From time to time I need to write dynamic scripts that need the current instance name and sometimes more specifically, the actually Computer Name that I am on in the cluster that I am working with. Here are a couple of helpful scripts help get me that information.
Some of the SERVERPROPERTY calls do not work on SQL Server 2008 R2 and you might need to default to the registry calls if you really need the data.
— gives you the ‘machine name': ‘SQL-MYINSTANCE’
SELECT SERVERPROPERTY (‘MachineName’)
— gives you the ‘server name': ‘SQL-MYINSTANCE\MYINSTANCE’
SELECT SERVERPROPERTY (‘ServerName’)
— gives you the local ‘ComputerName’ of the server in the cluster
SELECT SERVERPROPERTY(‘ComputerNamePhysicalNetBIOS’)
— this also gives you ‘physical machine name’ for the machine that you are running the command on: ‘S-TOL-SQL3′
DECLARE @dir VARCHAR(4000)
EXEC master.dbo.xp_regread
N’HKEY_LOCAL_MACHINE’,
N’SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName’,‘ComputerName’,
@dir output
SELECT @dir
— gets the default data path
SELECT SERVERPROPERTY(‘INSTANCEDEFAULTDATAPATH’)
— gets the default log path
SELECT SERVERPROPERTY(‘INSTANCEDEFAULTLOGPATH’)
— gets you the default backup path
DECLARE @dir VARCHAR(4000)
EXEC master.dbo.xp_instance_regread
N’HKEY_LOCAL_MACHINE’,
N’Software\Microsoft\MSSQLServer\MSSQLServer’, ‘BackupDirectory’,
@dir output
SELECT @dir
Image may be NSFW.
Clik here to view.

Clik here to view.
