Need to find Users logging Database in a week

Allen Francis 26 Reputation points
2023-01-25T14:39:40.2333333+00:00

There is a request to capture users and service account logging for particular Database and to send report in excel to the management. Requesting all to give a script if you have already or an idea how this can be accomplished.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,811 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Leandro Ezequiel Molinari 26 Reputation points
    2023-01-25T22:35:50.8833333+00:00

    Hi Allen, how are you?
    Here is a simple script that maybe can help you.

    The same uses a dynamic management view of the SQL server

    `USE [<dbname>] ---> Database Name

    go

    SELECT DISTINCT login_name

    FROM sys.dm_exec_sessions

    WHERE last_request_start_time >= DATEADD(week, -1, GETDATE())`


  2. Olaf Helper 41,001 Reputation points
    2023-01-26T06:54:19.0333333+00:00

    for particular Database

    Users/Accounts login to SQL Server, not to database. During the session they can change the context to the one or the other database. Or they use three-part qualifier to query data from different databases then the current one.

    In other words: It's not really possible.

    0 comments No comments

  3. Erland Sommarskog 101.8K Reputation points MVP
    2023-01-26T22:33:32.3633333+00:00

    I this is the same problem as your question? You are moving a database on this instance to a new instance? Or is it an unrelated request?

    I think you look at SQL Server Audit. I have not used it a lot, but you should be able to audit database access. But Olaf lists a couple of caveats that are relevant.

    0 comments No comments