Hi @Avyayah ,
Configure Login Auditing in SSMS, this list the logins in server level(instance level). After you enable the audit in SSMS, you need to restart the SQL server instance to make this change effect. You can get the result in error log as below screenshot in my environment.
> Found this script but since I restarted the server it lists todays login date
The information in the DMV will be cleared and nulled whenever you restart SQL Server.
From my knowledge, SQL server audit could not audit database level login user. If you want to get the last time SQL databases were accessed. Please using below T-SQL. Please note these values reset after SQL server restart.
; WITH STATS as (
SELECT D.name, D.database_id
, max(last_user_seek) as last_user_seek
, max(last_user_scan) as last_user_scan
, max(last_user_lookup) as last_user_lookup
, max(last_user_update) as last_user_update
FROM sys.databases D
LEFT OUTER JOIN sys.dm_db_index_usage_stats S on D.database_id = S.database_id
WHERE D.database_id > 4
AND S.object_id > 100
GROUP by D.name, D.database_id
)
SELECT * from STATS
UNION
SELECT D.name, D.database_id, NULL, NULL, NULL, NULL
FROM sys.databases D
WHERE D.database_id > 4
AND not exists (select * from STATS where name = D.name)
ORDER by 1
If the response is helpful, please click "Accept Answer" and upvote it, as this could help other community members looking for similar queries.