I need some help on how to find all the users that are connected to one of my database. I should check this on weekends to find out all the users that connected during the week.
Thank you.
auditconnectivitysql server
I need some help on how to find all the users that are connected to one of my database. I should check this on weekends to find out all the users that connected during the week.
Thank you.
Best Answer
You can check connected logins with the following query:
However, this query will only retrieve currenty connected sessions. if you would like to know which logins were logged into the server, you need to set up a trace, enable the audit for error log or use extended events.
Here's a solution to enable audit for error log:
Information will be stored on the Error Log. Data will be deleted over a time so make sure to review it's retention policy. To read the information on the error log you can use
sys.sp_readerrorlog
. The first parameter is the error log number (0 = current, 1 = first archive, etc.), second parameter determines the source to read (1 for error log) and the third parameter is the literal you want to filter info from:A more visual alternative to read the log is going to Management (on SSMS), then SQL Server Logs and double click on any log.
To see amount of logins by time you can use the following script (I'm using the "failed login" event because it's the one my server is tracking):
Result:
How to obtain user from these messages (this will highly depend on the message being shown on the error log):
Result:
Group by and count between a period:
Result: