Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • Rename Tablespace Oracle
  • How to remove blank lines using vi editor command Linux/Unix
  • How does one overcome the Unix 2 Gig file limit? Linux/Unix
  • Find_table_size.sql Oracle
  • For Search and replace unix command. Linux/Unix
  • metalink all dynamic view reference notes. Oracle
  • exp syntax in oracle 10g Oracle
  • currwait.sql Oracle
  • Temporary tablespace explaination Oracle
  • ENQ: KO – FAST OBJECT CHECKPOINT tips Oracle
  • How to find Unix server conguraiton including Server Model number and underlying disk sub-system ( On Solaris) Linux/Unix
  • This is im telling Kishore Oracle
  • Check Oracle installed products using one command Oracle
  • Transfer SQL Profiles from One database to other database. Oracle
  • Changing default shell Linux/Unix

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
  • Check SQL Server edition SQL Server
  • Checking SQL Server Version SQL Server
  • SQL Server: How to see current transactions or requests SQL Server
  • SQL Server Vs Oracle Architecture difference 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)
  • Django (0)
  • GIT (1)
  • Linux/Unix (149)
  • MYSQL (5)
  • Oracle (403)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (1)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (18)
  • rman-dataguard (26)
  • shell (150)
  • SQL scripts (350)
  • SQL Server (6)
  • Uncategorized (5)
  • Videos (0)

Recent Posts

  • Key Management in Oracle: The Core Issue: Missing Master Key12-May-2026
  • SAT Mathematics 10 questions and answer at the end.30-Apr-2026
  • top 10 AI news today30-Apr-2026
  • runon_allpdbs_show_conname.sh23-Apr-2026
  • runon_allcdbs_find_pdbs.sql23-Apr-2026
  • Running PDB on single node in RAC09-Apr-2026
  • find_arc.sql09-Apr-2026
  • pvm_pre_change.sql08-Apr-2026
  • find_encr_wallet.sql08-Apr-2026
  • find_pdbs.sql08-Apr-2026

Archives

  • 2026
  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • Is It Recommended To Apply Patch Bundles When PSU Is Available? -ID 743554.1 Oracle
  • Gather Stats manually using DBMS_STATS after disabling DBMS_SCHEDULER jobs as previous entry Oracle
  • How To Limit The Access To The Database So That Only One User Per Schema Are Connected (One Concurrent User Per Schema) Oracle
  • ORA-01220 Oracle
  • CTAS with LONG Column for 7.x and 8 and 8i Oracle
  • Space padding in korn shell Linux/Unix
  • Running PDB on single node in RAC Oracle
  • Generate SSH without password authentication. Linux/Unix

Copyright © 2026 pvmehta.com.

Powered by PressBook News WordPress theme