Posted By

hairajeshk on 08/04/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Tyster


Find users logged in to sql server in the previous days


 / Published in: SQL
 

To know who are the users logged in to sql server in te past previous days.

  1. SELECT DB_NAME(database_id), LastRead = MAX(CASE
  2. WHEN last_user_seek > last_user_scan AND last_user_seek > last_user_lookup
  3. THEN last_user_seek
  4. WHEN last_user_scan > last_user_seek AND last_user_scan > last_user_lookup
  5. THEN last_user_scan
  6. ELSE last_user_lookup
  7. END
  8. ), LastWrite = MAX(last_user_update) FROM
  9. (
  10. SELECT
  11. database_id,
  12. last_user_seek = COALESCE(last_user_seek, '19000101'),
  13. last_user_scan = COALESCE(last_user_scan, '19000101'),
  14. last_user_lookup = COALESCE(last_user_lookup, '19000101'),
  15. last_user_update = COALESCE(last_user_update, '19000101')
  16. FROM sys.dm_db_index_usage_stats
  17. ) x
  18. GROUP BY DB_NAME(database_id)
  19. ORDER BY 1;

Report this snippet  

You need to login to post a comment.