Get domain

  • Has anyone had to get the domain name from within T-SQL? I am in the situation where I would like to return the current domain of the server I'm on.


    Cheers,

    Ben Sullins
    bensullins.com
    Beer is my primary key...

  • "the current domain of the server I'm on"

    What do you mean?

    You need domain of the server or domain of the client?

    _____________
    Code for TallyGenerator

  • There is xp_inventory that retrieves sql server info, including domain name; check http://doc.ddart.net/mssql/sql70/xp_aa-sz_13.htm

    Otherwise you can retrieve the domain name from your win login name - create a function that retrieves the chars before '/' in the login name.

    Hope this helps.

     

  • try xp_cmdshell 'nslookup' this will give servername with domain state.

    Cheers,
    Sugeshkumar Rajendran
    SQL Server MVP
    http://sugeshkr.blogspot.com

  • ^^Does xp_sqlinventory exist in 2000 & 2005?

    Sugesh,

    We are not permitted to use xp_cmdshell for the security vulnerabilities which accompany it.


    Cheers,

    Ben Sullins
    bensullins.com
    Beer is my primary key...

  • couldn't find that extended proc on my SQL 2000; i believe it might be removed as part of a service pack, because i did find an article describing vulerabilities inolving this extended proc:

    http://www.appsecinc.com/resources/alerts/mssql/02-0000.html

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Try this:

    Declare @i Int,@Domain VarChar(256)

    Select

       @i=CharIndex('\',suser_sname()),

       @i=Case @i when 0 then 0 else @i-1 End,

       @Domain=Left(suser_sname(),@i)

    Print @Domain



    PeteK
    I have CDO. It's like OCD but all the letters are in alphabetical order... as they should be.

  • Thank you sir...this works perfect...


    Cheers,

    Ben Sullins
    bensullins.com
    Beer is my primary key...

  • Ben,

    next time please try to be more specific.

    People were trying to help you with server's domain name, but you're happy with Peter's solution which provides user's domain name.

    So, people wasted their time trying to help you from the very beginning.

    Next time they'll probably decide not to do so.

    _____________
    Code for TallyGenerator

  • It sounds like you've got a solution that works for you. On a side note (in case someone else comes across this thread) in a more complex situation, nslookup and using SUSER_SNAME() may not be effective. nslookup does a DNS name resolution, but is dependent on the domain search suffix order. So, for instance, if the SQL Server is on domainA.com but the search suffix says to search for domainZ.com first, you may get back server.domainZ.com back, depending on how the DNS/WINS servers are configured. SUSER_SNAME() will return the user's domain, but that's not necessarily the same domain as the server. The one that works (but requires sysadmin rights in SQL Server 2000 and by default in 2005) is:

    EXEC xp_loginconfig 'default domain'

     

    K. Brian Kelley
    @kbriankelley

  • Sergiy, it may not be that at all. If Ben's organization only has a single domain or if the users and servers are in the same domain, this would produce a correct result. In a more complex environment, you are correct that it potentially would not.

     

    K. Brian Kelley
    @kbriankelley

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply