July 14, 2008 at 3:12 am
Hi All,
How to get Current Instance Name of SQL Server from T-SQL (i.e. from Query window)?
Thanks in advance,
Regards,
Arun
July 14, 2008 at 3:19 am
Hey,
I am not sure if this is what you were looking for... but I can run this query to see the current server and all the linked servers on this instance of the server.
select * from sys.sysservers
Regards,
Prakash.P
---------------------------------------------------------------------------------
July 14, 2008 at 3:24 am
Hi,
Thanks for your update.
Yes. That's what I am looking for.
Thanks,
Regards,
Arun
July 14, 2008 at 3:26 am
SELECT SERVERPROPERTY ('InstanceName')
"Keep Trying"
July 14, 2008 at 4:31 am
SELECT @@SERVERNAME
July 14, 2008 at 5:04 am
--1.
SELECT SERVERPROPERTY('InstanceName')
--2
sp_helpserver
--3
select @@SERVERNAME
--4
SELECT * FROM SYS.SYSSERVERS
--5
SELECT * FROM SYS.SERVERS
July 10, 2009 at 10:57 am
Hi
All,
You better To use the following command for Getting the instance name.
EXECUTE xp_regread @rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQl',
@value_name='MSSQLSERVER'
Thanks and regards
Ashwin v p
Ashwin VP
CSC India ...
October 25, 2010 at 12:47 pm
I use this.
Select Case
When SERVERPROPERTY ('InstanceName') Is Null Then @@SERVERNAME
Else SERVERPROPERTY ('InstanceName')
End
October 26, 2010 at 12:29 am
Have a look at what Books online has to say about @@servername and serverproperty(). My recommendation is to use consistently only select convert(nvarchar(128), serverproperty('servername'));
Mixing the various alternatives for getting your server's name is a sure way to run into problems.
October 26, 2010 at 4:14 am
To get the "instance" name you can also do:
select @@ServiceName
June 28, 2011 at 3:57 am
@@ServiceName would be ok if there are named instances.
If none is available then select @@servicename results MSSQLSERVER.
So if you are interested in only the instance name, without the servername,
and replace instance name with servername, if there is no named instance then you could script:
HAVE FUN WITH IT.
declare @nameServer varchar(100)
declare @nameMachine varchar(50)
declare @kInstance varchar(50)
declare @constrServer varchar(100)
select @nameServer = convert(nvarchar(128), serverproperty('servername'));
select @nameMachine = convert(nvarchar(128), serverproperty('machinename'));
if len(@nameServer) = len(@nameMachine)
select @kInstance = '' -- 'NN'
else
select @kInstance = right(@nameServer, len(@nameServer) - (len(@nameMachine)+1));
-- construct name of server again from 2 variables:
if @kInstance='' select @constrServer = @nameMachine
else select @constrServer = @nameMachine + '\' + @kInstance;
select @nameServer as Sname, @nameMachine as Mname, @kInstance as kInst, @constrServer as cServ
-- if you are connected to a no named instance,
-- then this Qry should present you with the columns Sname, Mname and cServ as identical, kInst should be empty.
-- if you are connected to a named instance, then
-- this Qry should result in
-- Sname and cServ are identical
-- Mname is the machine name
-- kInst is the named instance.
November 7, 2011 at 12:21 pm
ashwin4all1 (7/10/2009)
HiAll,
You better To use the following command for Getting the instance name.
EXECUTE xp_regread @rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQl',
@value_name='MSSQLSERVER'
Thanks and regards
Ashwin v p
This is dead on the mark! Elegant and precise - thank you very much
Ted Bailey
Hospice of the Western Reserve
Cleveland, Ohio USA
November 7, 2011 at 5:17 pm
ashwin4all1 (7/10/2009)
HiAll,
You better To use the following command for Getting the instance name.
EXECUTE xp_regread @rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQl',
@value_name='MSSQLSERVER'
Thanks and regards
Ashwin v p
Why is that better?
--Jeff Moden
Change is inevitable... Change for the better is not.
November 15, 2011 at 10:07 pm
ChiragNS (7/14/2008)
SELECT SERVERPROPERTY ('InstanceName')
this helped me.. thanks
January 30, 2013 at 9:38 am
Really great solution !
a.zimnik (6/28/2011)
@@ServiceName would be ok if there are named instances.If none is available then select @@servicename results MSSQLSERVER.
So if you are interested in only the instance name, without the servername,
and replace instance name with servername, if there is no named instance then you could script:
HAVE FUN WITH IT.
declare @nameServer varchar(100)
declare @nameMachine varchar(50)
declare @kInstance varchar(50)
declare @constrServer varchar(100)
select @nameServer = convert(nvarchar(128), serverproperty('servername'));
select @nameMachine = convert(nvarchar(128), serverproperty('machinename'));
if len(@nameServer) = len(@nameMachine)
select @kInstance = '' -- 'NN'
else
select @kInstance = right(@nameServer, len(@nameServer) - (len(@nameMachine)+1));
-- construct name of server again from 2 variables:
if @kInstance='' select @constrServer = @nameMachine
else select @constrServer = @nameMachine + '\' + @kInstance;
select @nameServer as Sname, @nameMachine as Mname, @kInstance as kInst, @constrServer as cServ
-- if you are connected to a no named instance,
-- then this Qry should present you with the columns Sname, Mname and cServ as identical, kInst should be empty.
-- if you are connected to a named instance, then
-- this Qry should result in
-- Sname and cServ are identical
-- Mname is the machine name
-- kInst is the named instance.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply