时间:2021-07-01 10:21:17 帮助过:56人阅读
一、查看實例名時可用
1、服务—SQL Server(实例名),默认实例为(MSSQLSERVER)
或在连接企业管理时-查看本地实例
2、通過注冊表
HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Microsoft SQL Server/InstalledInstance
3、用命令
sqlcmd/osql
sqlcmd -L
sqlcmd -Lc
osql -L
获取可用實例,以下舉一個例子,根據自己情況改
insert @Table EXEC sys.xp_cmdshell 'sqlcmd -Lc'
--LEFT(@@serverName,CHARINDEX('/',@@serverName+'/')-1) 替代為本機名就行了 , 根據實例命名規則判斷
SELECT * FROM @Table WHERE instanceName LIKE LEFT( @@serverName , CHARINDEX ( '/' , @@serverName + '/' )- 1)+ '%'
--1.
SELECT SERVERPROPERTY('InstanceName')
--2
sp_helpserver
--3
select @@SERVERNAME
--4
SELECT * FROM SYS.SYSSERVERS
--5
SELECT * FROM SYS.SERVERS
三、
EXECUTE xp_regread @rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE/Microsoft/Microsoft SQL Server/Instance Names/SQl',
@value_name='MSSQLSERVER'
四、
Select Case
When SERVERPROPERTY ('InstanceName') Is Null Then @@SERVERNAME
Else SERVERPROPERTY ('InstanceName')
End
五、在本地或网络得到所有实例名
1、You can do with registry reading , like my code
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);
}
}
}
}
}
var sqlServers = new List();
try
{
app = new ApplicationClass();
sqlNameList = app.ListAvailableSQLServers();
foreach (string sqlServer in sqlNameList)
sqlServers.Add(sqlServer);
}
catch(Exception ex)
{
//play with the exception.
}
finally
{
if (sqlNameList != null)
sqlNameList = null;
if (app != null)
app = null;
}
return sqlServers;
}