我试图确定我已经安装的sql server/sql express的实例(手动或编程),但所有的例子都告诉我运行一个sql查询来确定这假设我已经连接到一个特定的实例。


当前回答

安装的所有实例都应该显示在Microsoft管理控制台的服务管理单元中。如果需要获取实例名,请执行“开始|运行|类型的服务”。查找所有带有“Sql Server(实例名)”的条目。

其他回答

命令OSQL -L和sqlmd -L将显示网络上的所有实例。

如果你想要一个服务器上所有实例的列表,并且不喜欢编写脚本或编程,可以这样做:

启动Windows任务管理器 勾选“显示来自所有用户的进程”或相同的复选框 按照“映像名称”对进程进行排序 找到所有sqlsrvr.exe映像

实例应该在“用户名”列中以MSSQL$INSTANCE_NAME的形式列出。

我从认为这个可怜的服务器正在运行63个实例,到意识到它正在运行3个实例(其中一个在CPU负载方面表现得完全像个恶霸……)

在Windows命令行中输入:

SC \\server_name query | find /I "SQL Server ("

其中“server_name”是希望在其上显示SQL实例的任何远程服务器的名称。

当然,这需要足够的权限。

安装的所有实例都应该显示在Microsoft管理控制台的服务管理单元中。如果需要获取实例名,请执行“开始|运行|类型的服务”。查找所有带有“Sql Server(实例名)”的条目。

当我评估100多个服务器时,我也遇到了同样的问题,我用c#写了一个脚本来浏览由SQL组成的服务名称。当在服务器上安装实例时,SQL server为每个实例添加一个服务,并使用服务名。对于不同的版本,例如2000年到2008年,它可能有所不同,但肯定有一个具有实例名的服务。

我获取服务名并从服务名获得实例名。下面是WMI查询结果的示例代码:

if (ServiceData.DisplayName == "MSSQLSERVER" || ServiceData.DisplayName == "SQL Server (MSSQLSERVER)")
            {
                InstanceData.Name = "DEFAULT";
                InstanceData.ConnectionName = CurrentMachine.Name;
                CurrentMachine.ListOfInstances.Add(InstanceData);
            }
            else
                if (ServiceData.DisplayName.Contains("SQL Server (") == true)
                {
                    InstanceData.Name = ServiceData.DisplayName.Substring(
                                            ServiceData.DisplayName.IndexOf("(") + 1,
                                            ServiceData.DisplayName.IndexOf(")") - ServiceData.DisplayName.IndexOf("(") - 1
                                        );
                    InstanceData.ConnectionName = CurrentMachine.Name + "\\" + InstanceData.Name;
                    CurrentMachine.ListOfInstances.Add(InstanceData);
                }
                else
                    if (ServiceData.DisplayName.Contains("MSSQL$") == true)
                    {
                        InstanceData.Name = ServiceData.DisplayName.Substring(
                                                ServiceData.DisplayName.IndexOf("$") + 1,
                                                ServiceData.DisplayName.Length - ServiceData.DisplayName.IndexOf("$") - 1
                                            );

                        InstanceData.ConnectionName = CurrentMachine.Name + "\\" + InstanceData.Name;
                        CurrentMachine.ListOfInstances.Add(InstanceData);
                    }

If you just want to see what's installed on the machine you're currently logged in to, I think the most straightforward manual process is to just open the SQL Server Configuration Manager (from the Start menu), which displays all the SQL Services (and only SQL services) on that hardware (running or not). This assumes SQL Server 2005, or greater; dotnetengineer's recommendation to use the Services Management Console will show you all services, and should always be available (if you're running earlier versions of SQL Server, for example).

If you're looking for a broader discovery process, however, you might consider third party tools such as SQLRecon and SQLPing, which will scan your network and build a report of all SQL Service instances found on any server to which they have access. It's been a while since I've used tools like this, but I was surprised at what they found (namely, a handful of instances that I didn't know existed). YMMV. You might Google for details, but I believe this page has the relevant downloads: http://www.sqlsecurity.com/Tools/FreeTools/tabid/65/Default.aspx