How to get current instance name from T-SQL

后端 未结 8 1024
天命终不由人
天命终不由人 2021-01-30 05:18

How can I get the SQL Server server and instance name of the current connection, using a T-SQL script?

8条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-30 05:23

    I found this:

    EXECUTE xp_regread
            @rootkey = 'HKEY_LOCAL_MACHINE',
            @key = 'SOFTWARE\Microsoft\Microsoft SQL Server',
            @value_name = 'InstalledInstances'
    

    That will give you list of all instances installed in your server.


    The ServerName property of the SERVERPROPERTY function and @@SERVERNAME return similar information. The ServerName property provides the Windows server and instance name that together make up the unique server instance. @@SERVERNAME provides the currently configured local server name.

    And Microsoft example for current server is:

    SELECT CONVERT(sysname, SERVERPROPERTY('servername'));
    

    This scenario is useful when there are multiple instances of SQL Server installed on a Windows server, and the client must open another connection to the same instance used by the current connection.

提交回复
热议问题