TSQL: Get Last Queries Ran

SqlTsqlHistory

Sql Problem Overview


Is there a way to get the SQL text for the last few queries?

I am using Microsoft SQL Server 2005

Sql Solutions


Solution 1 - Sql

Yes, take a look, this will give you the 50 most recent executed SQL statements

sql 2005 and up only

SELECT TOP 50 * FROM(SELECT COALESCE(OBJECT_NAME(s2.objectid),'Ad-Hoc') AS ProcName,
  execution_count,s2.objectid,
    (SELECT TOP 1 SUBSTRING(s2.TEXT,statement_start_offset / 2+1 ,
      ( (CASE WHEN statement_end_offset = -1
  THEN (LEN(CONVERT(NVARCHAR(MAX),s2.TEXT)) * 2)
ELSE statement_end_offset END)- statement_start_offset) / 2+1)) AS sql_statement,
       last_execution_time
FROM sys.dm_exec_query_stats AS s1
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2 ) x
WHERE sql_statement NOT like 'SELECT TOP 50 * FROM(SELECT %'
--and OBJECTPROPERTYEX(x.objectid,'IsProcedure') = 1
ORDER BY last_execution_time DESC

Solution 2 - Sql

If using SQL Server 2005+:

SELECT deqs.last_execution_time AS [Time], dest.TEXT AS [Query]
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY deqs.last_execution_time DESC

Great tip from SQLAuthority!

Solution 3 - Sql

The only way I'm aware of is to have the SQL Server Profiler running. Unfortunately this needs to be started prior to the queries being executed, so if you're hoping to catch something that's happened on an "ad hoc" basis, it won't be suitable. If you're trying to track what a piece of code's doing and want to capture the queries it executes, it should work a treat.

Solution 4 - Sql

If you need to inspect parameter values, this addition returns the <ParameterList> XML

SELECT TOP 50 * FROM(SELECT COALESCE(OBJECT_NAME(s2.objectid),'Ad-Hoc') AS ProcName,
  execution_count,s2.objectid,
    (SELECT TOP 1 SUBSTRING(s2.TEXT,statement_start_offset / 2+1 ,
      ( (CASE WHEN statement_end_offset = -1
  THEN (LEN(CONVERT(NVARCHAR(MAX),s2.TEXT)) * 2)
ELSE statement_end_offset END)- statement_start_offset) / 2+1)) AS sql_statement,
  SUBSTRING(
    s3.query_plan,CHARINDEX('<ParameterList>',s3.query_plan),
    CHARINDEX('</ParameterList>',s3.query_plan) + LEN('</ParameterList>') - CHARINDEX('<ParameterList>',s3.query_plan)
  ) AS Parameters,
  last_execution_time
FROM sys.dm_exec_query_stats AS s1
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2
CROSS APPLY sys.dm_exec_text_query_plan(s1.plan_handle, s1.statement_start_offset,
  s1.statement_end_offset) AS s3
) x
WHERE sql_statement NOT like 'SELECT TOP 50 * FROM(SELECT %'
ORDER BY last_execution_time DESC

Prettified version

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser295190View Question on Stackoverflow
Solution 1 - SqlSQLMenaceView Answer on Stackoverflow
Solution 2 - SqlDustin LaineView Answer on Stackoverflow
Solution 3 - SqlRobView Answer on Stackoverflow
Solution 4 - SqlDavid VaughanView Answer on Stackoverflow