Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to view and get the instance name in SQL Server

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to view and obtain instance names in SQL Server. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1. Available when viewing the practice name

1. Service-SQL Server (instance name). The default instance is (MSSQLSERVER).

Or when connecting to enterprise management-view the local instance

2. Pass the note table HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Microsoft SQL Server/InstalledInstance

3. Use the command sqlcmd/osqlsqlcmd-Lsqlcmd-Lcosql-L

To get the available practical examples, here is an example. Copy the code according to your situation as follows: DECLARE @ Table TABLE (instanceName sysname NULL)

Insert @ Table EXEC sys.xp_cmdshell 'sqlcmd-Lc'

-- LEFT (@ @ serverName,CHARINDEX ('/', @ @ serverName+'/')-1) can be replaced by the local name, which is based on the naming rule of practice.

SELECT * FROM @ Table WHERE instanceName LIKE LEFT (@ @ serverName, CHARINDEX ('/', @ @ serverName +'/)-1) +'%'

II.

-1. SELECT SERVERPROPERTY ('InstanceName')

-- 2sp_helpserver

-- 3select @ @ SERVERNAME

-- 4SELECT * FROM SYS.SYSSERVERS

-- 5SELECT * FROM SYS.SERVERS

3.

EXECUTE xp_regread @ rootkey='HKEY_LOCAL_MACHINE',@key='SOFTWARE/Microsoft/Microsoft SQL Server/Instance Names/SQl',@value_name='MSSQLSERVER'

IV.

Select CaseWhen SERVERPROPERTY ('InstanceName') Is Null Then @ @ SERVERNAMEElse SERVERPROPERTY (' InstanceName') End

5. Get all instance names locally or on the network

1. You can do with registry reading and like my code copy the 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 、 You can use SQLDMO.dll to retrieve the list of SQL Server instances. The SQLDMO.dll can be found from the "C:/Program Files/Microsoft SQL Server/80/Tools/Bin" folder. Refer this assembly in your project and the following snippet would return a List Object containing the sql server instances. The copy code is 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 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;}

So much for sharing about how to view and obtain instance names in SQL Server. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report