Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get some data from the Active Directory in SQL. I use the code:

SELECT * FROM OpenQuery 
'SELECT  objectGUID, objectSID, employeeID, lastLogon, mobile, telephoneNumber, mail,  department, givenName, sn, displayname,  sAmAccountName
FROM ''LDAP://DC=mycompany''    
where  objectClass = ''user''
and sAmAccountName = ''testuser'''  

My problem is that the value i receive for lastLogon isn't refreshing. It is possible that the other values aren't also refreshing, however i can't say this for sure, because in the other data there is no change, but the lastLogon value should be updated when i lock my computer and log in.

Does anyone have any idee why is this, and how can i solve this?


share|improve this question

migrated from Feb 1 '12 at 14:07

This question came from our site for professional and enthusiast programmers.

up vote 2 down vote accepted

The lastLogon attribute reflects the last interactive logon, not the last network-based logon

More info found here

This is some excellent information from this site

Another twist: Active Directory does NOT replicate the LastLogon attribute across domain controllers. So in order to get an accurate value, you need to obtain the LastLogon value for the same user from all your domain controllers and accept the value that is highest.

share|improve this answer
if i make a query from a C++ application then it is updated. Why is that? – kampi Feb 1 '12 at 13:37
@kampi: IIRC, An ADSI linked server hits the same DC, not the domain itself. – gbn Feb 1 '12 at 13:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.