Solved

finding recently run statements

Posted on 2011-02-16
5
220 Views
Last Modified: 2012-05-11
i had run some recent statement from query window.. but i am trying to track exact parameters i sent.. but i could not see it from the below query..

there are a bunch of statements even from a week before- so i know memory is not the issue.. what am i missing.. isn't every select and execute statement cached?

thanks
SELECT  text,last_execution_time
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
WHERE last_execution_time between 
'2011-02-16 13:00:00.000' and '2011-02-16 14:45:00.000'
and text like '%sp_send_dbmail%'
ORDER BY qs.last_execution_time DESC

Open in new window

0
Comment
Question by:anushahanna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
lcohan earned 500 total points
ID: 34910957
I believe not all statements will stay in cache forever even if lets say you have infinite memory.
If you look in particular for your sent mail please run following

select * from msdb..sysmail_unsentitems

select top 100 * from msdb..sysmail_sentitems order by sent_date desc
0
 
LVL 40

Expert Comment

by:lcohan
ID: 34911006
Or just run this without date limit:

SELECT  top 100 text,last_execution_time
      FROM sys.dm_exec_query_stats qs
            CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt
            CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
      WHERE text like '%sp_send_dbmail%'
      ORDER BY qs.last_execution_time DESC
0
 
LVL 6

Author Comment

by:anushahanna
ID: 34935856
lcohan, still could not get it.. even though there is entry from before when these were written. do this come under your category you mentioned "I believe not all statements will stay in cache"- if so, what might they be?
0
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 500 total points
ID: 34954707
Sorry I created more confusion than helped. Here, please run the query below and have a look at the creation_date vs. last_execution_time. They will be different for objects planned and stored in cache at the creation_time and last executed on last_execution_time - this should be pretty strait forward.
The objects you see with old creation_time were planned/stored in cache since and what I'm trying to say is that not ALL objects will stay in cache forever or for for the same duration. You could see the query plan if you click the link on the last column.

SELECT top 100 text,creation_time,last_execution_time,query_plan
    FROM sys.dm_exec_query_stats qs
       CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt
       CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp

Now, if you look in particular for "sp_send_dbmail" queries then you could find all sent/unsent mail info in the two: msdb..sysmail_unsentitems and msdb..sysmail_sentitems
0
 
LVL 6

Author Comment

by:anushahanna
ID: 34985881
thanks lcohan- for your detailed query.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Merige returns error code when updating 15 60
kill process lock Sql server 9 81
What is this datetime? 1 33
Database maintenance 36 141
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question