Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2002
    Posts
    22

    Unanswered: how to get hostname via SQL command?

    Hi!

    Can something tell me how I can obtain the machine name (either IP address or DNS name) on which SQL Server is running via a SQL query?

    select @@version returns a lot of useful information, however it doesn't return the host name.

    thanks in advance
    - Garry

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I haven't tested this, and it requires sysadmin privleges to run, but couldn't you use:
    Code:
    EXECUTE master.dbo.xp_cmdshell 'SET COMPUTERNAME'
    -PatP

  3. #3
    Join Date
    Feb 2009
    Posts
    3
    select @@servername

    this came up on a Google search, so I decided to put up the correct answer.

  4. #4
    Join Date
    Aug 2006
    Location
    San Francisco, CA
    Posts
    136
    you can also run:

    Code:
    select SERVERPROPERTY ('ServerName')
    Check out: SERVERPROPERTY for more info

  5. #5
    Join Date
    Aug 2008
    Location
    India
    Posts
    55
    SQL server name may be different from hostname just fyi

  6. #6
    Join Date
    Feb 2009
    Posts
    1

    reply

    select HOST_NAME ( ) - This would give the machine name on which the sql server is running.

  7. #7
    Join Date
    Aug 2008
    Location
    India
    Posts
    55
    Please dont try this from Local client it gives PC name of the desktop from which i am using - plz try this inside SQL server of same machine

  8. #8
    Join Date
    Apr 2007
    Posts
    183

  9. #9
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    Quote Originally Posted by rajini_kesavelu
    select HOST_NAME ( ) - This would give the machine name on which the sql server is running.
    Actually, that gives you the name of the client from which you have connected.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •