Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • scp with ssh2 Linux/Unix
  • How to see which patches are applied. Oracle
  • Best approach for Oracle database patching sequence to latest/required patchset along with CPU/PSU/any-other-one-off patch ID 865255.1 Oracle
  • sid_wise_sql.sql Further explaination Oracle
  • JSON/XML Types in Oracle Oracle
  • to see when crontab is changed. Linux/Unix
  • create database link syntax Oracle
  • Oracle 10g Wait Model Oracle
  • Changing default shell Linux/Unix
  • Roles and Stored Procs II Oracle
  • V$INSTANCE of Oracle in MYSQL MYSQL
  • How to Use DBMS_STATS to Move Statistics to a Different Database Oracle
  • shutdown linux Linux/Unix
  • pvm_pre_change.sql Oracle
  • sbind.sql Find Bind variable from sql_id sqlid Oracle

SQL Server: How to see historical transactions

Posted on 25-Jul-202525-Jul-2025 By Admin No Comments on SQL Server: How to see historical transactions

You can determine if a SQL Server database is actively being used by any users, both currently and historically (to some extent).


βœ… Part 1: Check Current Active Usage

πŸ”Ή 1. Who Is Connected Right Now (to a DB)?

SELECT
    s.session_id,
    s.login_name,
    s.host_name,
    s.program_name,
    r.status,
    s.database_id,
    DB_NAME(s.database_id) AS database_name,
    r.command,
    r.start_time
FROM sys.dm_exec_sessions s
JOIN sys.dm_exec_requests r ON s.session_id = r.session_id
WHERE s.database_id = DB_ID('YourDatabaseName');

πŸ” Shows who is currently active in a specific database, with login, host, and command info.


πŸ”Ή 2. See All Sessions Using Any DB (Group Count)

SELECT
    DB_NAME(database_id) AS database_name,
    COUNT(session_id) AS active_sessions
FROM sys.dm_exec_sessions
WHERE database_id IS NOT NULL
GROUP BY database_id;

πŸ” Quick snapshot to see which databases have active sessions.


πŸ”Ή 3. Check If Any Process Accessed DB Recently (Last X Minutes)

SELECT 
    DB_NAME(database_id) AS database_name,
    last_request_start_time,
    last_request_end_time,
    login_name,
    host_name,
    program_name
FROM sys.dm_exec_sessions
WHERE database_id = DB_ID('YourDatabaseName')
  AND last_request_end_time > DATEADD(MINUTE, -30, GETDATE());

πŸ” See last request timestamps by user/session.


βœ… Part 2: Check Recent/Historical Usage (Basic Auditing)

πŸ”Ή 4. Use Default Trace (Lightweight Built-in Logging)

SELECT 
    te.name AS event_name,
    t.DatabaseName,
    t.LoginName,
    t.ApplicationName,
    t.HostName,
    t.StartTime
FROM sys.fn_trace_gettable(CONVERT(VARCHAR(150), 
    (SELECT TOP 1 value FROM sys.fn_trace_getinfo(NULL) WHERE property = 2)), DEFAULT) t
JOIN sys.trace_events te ON t.EventClass = te.trace_event_id
WHERE t.DatabaseName = 'YourDatabaseName'
ORDER BY t.StartTime DESC;

βœ… Use this to see recent schema changes or logins involving the DB.


πŸ”Ή 5. Use SQL Audit (if previously configured)

If SQL Audit was enabled on the database:

SELECT event_time, action_id, session_server_principal_name, statement
FROM sys.fn_get_audit_file('C:\AuditLogs\*.sqlaudit', DEFAULT, DEFAULT)
WHERE database_name = 'YourDatabaseName';

βœ… Part 3: Monitor Long-Term Usage Trends

If you want to track over time whether a DB is being used:

πŸ”Ή Setup a Light Agent Job

-- Run every X minutes
INSERT INTO db_usage_log (db_name, username, login_time, last_activity)
SELECT 
    DB_NAME(s.database_id), 
    s.login_name,
    s.login_time,
    s.last_request_end_time
FROM sys.dm_exec_sessions s
WHERE s.database_id = DB_ID('YourDatabaseName');

Store in a table like db_usage_log, then query/report later.


🚨Is Database Totally Idle?

To see if no one has touched the database in days:

SELECT 
    name AS database_name,
    last_user_seek,
    last_user_scan,
    last_user_lookup,
    last_user_update
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID('YourDatabaseName');

If all columns are NULL, it’s likely the DB is not in use at all.


βœ… Summary Table

GoalQuery / Method
Who’s using DB now?sys.dm_exec_sessions + sys.dm_exec_requests
When was DB last used?last_request_end_time, index_usage_stats
Recent access by userdefault trace, fn_trace_gettable()
Monitor usage over timeScheduled job inserting from dm_exec_sessions
Auditing accessSQL Audit (if configured)

SQL Server

Post navigation

Previous Post: SQL Server: How to see current transactions or requests
Next Post: SQL Server Vs Oracle Architecture difference

Related Posts

  • Check SQL Server edition SQL Server
  • SQL Server Vs Oracle Architecture difference SQL Server
  • SQL Server: How to see current transactions or requests SQL Server
  • Checking SQL Server Version SQL Server
  • T-SQL Vs PL/SQL Syntax SQL Server

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Ansible (0)
  • AWS (2)
  • Azure (1)
  • Linux/Unix (149)
  • MYSQL (5)
  • Oracle (393)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (0)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (17)
  • rman-dataguard (26)
  • shell (149)
  • SQL scripts (342)
  • SQL Server (6)
  • Uncategorized (0)
  • Videos (0)

Recent Posts

  • Trace a SQL session from another session using ORADEBUG30-Sep-2025
  • SQL Server Vs Oracle Architecture difference25-Jul-2025
  • SQL Server: How to see historical transactions25-Jul-2025
  • SQL Server: How to see current transactions or requests25-Jul-2025
  • T-SQL Vs PL/SQL Syntax25-Jul-2025
  • Check SQL Server edition25-Jul-2025
  • Checking SQL Server Version25-Jul-2025
  • Oracle vs MYSQL Architecture differences (For DBAs)24-Jul-2025
  • V$INSTANCE of Oracle in MYSQL24-Jul-2025
  • Day to day MYSQL DBA operations (Compared with Oracle DBA)24-Jul-2025

Archives

  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • useful dg links Oracle
  • oracle_env_10g_CADEV Linux/Unix
  • SQL_PLAN.sql for checking real execution plan Oracle
  • Day to day MYSQL DBA operations (Compared with Oracle DBA) MYSQL
  • scripts to take listener.log backup Linux/Unix
  • Renaming Global Name GLOBAL_NAME Oracle
  • ORA-8031 issue and solution if it is occuring due to truncate. Oracle
  • checking connectivity between two servers Linux/Unix

Copyright © 2025 pvmehta.com.

Powered by PressBook News WordPress theme