Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Get The Domain Name Of Your SQL Server Machine With T-SQL

From Wiki

Jump to: navigation, search

How do you get the domain name that your SQL Server machine belongs to with T-SQL code?

You can use the undocumented procedure xp_regread; I have tested this on SQL Server 2005 and on SQL Server 2000. Be advised that since this is an undocumented procedure that it might not be available in the future or it might be changed either because of a service pack or a hot fix. Don't use it as production code!

Here is how to do this

  1. DECLARE @chvDomainName NVARCHAR(100)
  2. EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',
  3. 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon',
  4. N'CachePrimaryDomain',@chvDomainName OUTPUT
  6. SELECT @chvDomainName AS DomainName

Contributed by:--SQLDenis 16:46, 4 June 2008 (GMT)

Part of SQL Server Admin Hacks

334 Rating: 2.8/5 (56 votes cast)