When was my db last used?

  • I am running this script: and get an error message:Msg 170, Level 15, State 1, Line 5

    Line 5: Incorrect syntax near 'max'.

    SELECT s2.dbid,

    s1.sql_handle,

    (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,

    execution_count,

    plan_generation_num,

    last_execution_time,

    total_worker_time,

    last_worker_time,

    min_worker_time,

    max_worker_time,

    total_physical_reads,

    last_physical_reads,

    min_physical_reads,

    max_physical_reads,

    total_logical_writes,

    last_logical_writes,

    min_logical_writes,

    max_logical_writes

    FROM sys.dm_exec_query_stats AS s1

    CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2

    WHERE s2.objectid is null

    ORDER BY s1.sql_handle, s1.statement_start_offset, s1.statement_end_offset

    Please help me. Thank you

  • Which version of SQL are u using??

  • 2005

  • no problem at my end (on both 2008 and 2005 SP2)

    SELECT s2.dbid,

    s1.sql_handle,

    (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,

    execution_count,

    plan_generation_num,

    last_execution_time,

    total_worker_time,

    last_worker_time,

    min_worker_time,

    max_worker_time,

    total_physical_reads,

    last_physical_reads,

    min_physical_reads,

    max_physical_reads,

    total_logical_writes,

    last_logical_writes,

    min_logical_writes,

    max_logical_writes

    FROM sys.dm_exec_query_stats AS s1

    CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2

    WHERE s2.objectid IS NULL

    ORDER BY s1.sql_handle, s1.statement_start_offset, s1.statement_end_offset

    SQLServerNewbieMCITP: Database Administrator SQL Server 2005
  • Check the compat mode of the DB you're running that in. My guess would be that it's in mode 80. Either change the compat mode or run the query from master (as both of those DMVs are server-wide, not database specific)

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply