July 12, 2007 at 5:07 pm
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.
Ben Sullins
bensullins.com
Beer is my primary key...
July 12, 2007 at 7:07 pm
"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
July 12, 2007 at 10:48 pm
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.
July 12, 2007 at 11:35 pm
try xp_cmdshell 'nslookup' this will give servername with domain state.
Cheers,
Sugeshkumar Rajendran
SQL Server MVP
http://sugeshkr.blogspot.com
July 13, 2007 at 9:20 am
^^Does xp_sqlinventory exist in 2000 & 2005?
Sugesh,
We are not permitted to use xp_cmdshell for the security vulnerabilities which accompany it.
Ben Sullins
bensullins.com
Beer is my primary key...
July 13, 2007 at 11:18 am
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
July 16, 2007 at 9:49 am
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
July 16, 2007 at 10:22 am
Thank you sir...this works perfect...
Ben Sullins
bensullins.com
Beer is my primary key...
July 16, 2007 at 3:25 pm
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
July 17, 2007 at 4:46 pm
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
July 17, 2007 at 4:48 pm
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