HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Getting SQL Server query history

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
historysqlquerygettingserver

Problem

I have used below query to get the history queries on ran SQL Server but it's not giving me the whole history it just returns queries ran on nearly about last 1/2 an hour. I want to get the queries ran on week ago. Is there any solution?

SELECT  dest.text, deqs.last_execution_time
FROM    sys.dm_exec_query_stats AS deqs
        CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
WHERE    dest.text NOT LIKE '%dest.text%' 
         order by deqs.last_execution_time desc

Solution

I believe this is handled internally in SQL Server's query cache using memory available on the system. I know you can empty all of the cache by running the following:

DBCC FREEPROCCACHE WITH NO_INFOMSGS;


I just don't think you can alter how many queries it's saving.

Good luck.

Code Snippets

DBCC FREEPROCCACHE WITH NO_INFOMSGS;

Context

StackExchange Database Administrators Q#33721, answer score: 3

Revisions (0)

No revisions yet.