Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • Consolidated Reference List Of Notes For Migration / Upgrade Service Requests -ID 762540.1 Oracle
  • good linux notes Linux/Unix
  • Standby Database File Management in 10g with STANDBY_FILE_MANAGEMENT Oracle
  • find checksum of a file. Linux/Unix
  • get_aix_vmstat.ksh Oracle
  • Read CSV file using PySpark Python/PySpark
  • Jai Shree Ram Oracle
  • find_open_cur.sql Find open cursorts per session Oracle
  • JSON/XML Types in Oracle Oracle
  • Reading parameter file and printing Linux/Unix
  • Privilege to describe the table. Oracle
  • currwait.sql Oracle
  • Goldengate document from Porus Oracle
  • runsql_once.ksh Linux/Unix
  • S3 Basic info AWS

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

  • T-SQL Vs PL/SQL Syntax SQL Server
  • SQL Server Vs Oracle Architecture difference SQL Server
  • Check SQL Server edition SQL Server
  • SQL Server: How to see current transactions or requests SQL Server
  • Checking SQL Server Version 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 (392)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (0)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (17)
  • rman-dataguard (26)
  • shell (149)
  • SQL scripts (341)
  • SQL Server (6)
  • Uncategorized (0)
  • Videos (0)

Recent Posts

  • 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
  • MYSQL and Oracle Comparison for Oracle DBA24-Jul-2025

Archives

  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • get_vmstat_solaris Oracle
  • mutex in Oracle 10.2.0.2 or Oracle 10g Oracle
  • Removing Blank lines from file using grep Linux/Unix
  • Sample WW22 listener.ora Oracle
  • Monitor Long Running Job Oracle
  • How to find the real execution plan and binds used in that explain plan in Oracle 10g?? Oracle
  • Default User Profile Oracle
  • Unix split command to split files Linux/Unix

Copyright © 2025 pvmehta.com.

Powered by PressBook News WordPress theme