First, see the example name available
1, the service-sql Server (instance name), the default instance is (MSSQLSERVER)
Or when connecting to enterprise management-View local instances
2. Through the brochure form
Hkey_local_machine/software/microsoft/microsoft SQL server/installedinstance
3, with the command
Sqlcmd/osql
Sqlcmd-l
Sqlcmd-lc
Osql-l
To get the available examples, here's an example of how you can change your situation
Copy Code code as follows:
DECLARE @Table Table (instancename sysname NULL)
Insert @Table EXEC sys.xp_cmdshell ' SQLCMD-LC '
--left (@ @serverName, CHARINDEX ('/', @ @serverName + '/')-1) instead of the name of the machine, according to the case naming rules
SELECT * from @Table WHERE instancename like left (@ @serverName, CHARINDEX ('/', @ @serverName + '/')-1) + '% '
Second,
--1.
SELECT serverproperty (' InstanceName ')
--2
Sp_helpserver
--3
SELECT @ @SERVERNAME
--4
SELECT * from SYS. sysservers
--5
SELECT * from SYS. SERVERS
Three
EXECUTE xp_regread @rootkey = ' HKEY_LOCAL_MACHINE ',
@key = ' Software/microsoft/microsoft SQL server/instance names/sql ',
@value_name = ' MSSQLServer '
Four
Select case
When Serverproperty (' InstanceName ') is Null Then @ @SERVERNAME
Else serverproperty (' InstanceName ')
End
V. Get all instance names locally or on the network
1, can do with registry reading, like my Code
Copy Code code as follows:
Using System;
Using Microsoft.Win32;
Namespace Smotest
{
Class Program
{
static void Main ()
{
RegistryKey RK = Registry.LocalMachine.OpenSubKey (@ "Software/microsoft/microsoft sql Server");
String[] instances = (string[]) rk. GetValue ("InstalledInstances");
if (instances. Length > 0)
{
foreach (String element in instances)
{
if (element = = "MSSQLServer")
Console.WriteLine (System.Environment.MachineName);
Else
Console.WriteLine (System.Environment.MachineName + @ "/" + Element);
}
}
}
}
}
2. Can use SQLDMO.dll to retrieve the list of SQL Server instances. The SQLDMO.dll can is found from the "C:/Program files/microsoft SQL server/80/tools/bin" folder. Refer this assembly into your project and the following snippet would return a List Object containing the SQL Server Instanc Es.
Copy Code code as follows:
public static List Getsqlserverinstances ()
{
NameList sqlnamelist = null;
Application app = null;
var sqlservers = new List ();
Try
{
App = new ApplicationClass ();
Sqlnamelist = App. ListAvailableSQLServers ();
foreach (String SQL Server in Sqlnamelist)
Sqlservers.add (SQL Server);
}
catch (Exception ex)
{
Play with the exception.
}
Finally
{
if (sqlnamelist!= null)
Sqlnamelist = null;
if (app!= null)
App = null;
}
return sqlservers;
}