Result set to eventvwr I'm looking for a solution to the following case:

I have a trace(only captures queries above 5000ms duration, which is about 20 per hour) running in MSSQL which I can query by using:

SELECT TextData, Duration, CPU, Reads, Writes
FROM fn_trace_gettable(N'C:\temp\TRACE.trc', DEFAULT)
WHERE EndTime &gt; DATEADD(MI,-15,GETDATE()) AND Reads &lt; 10 AND Writes &lt; 10


I want this query to run this query every 15 minutes and the results should be written(each result in it's own event log) to the eventvwr using xp_logevent. Our monitor software will then trigger on the results of the Event Viewer.

I tried to achieve this by putting the results in a temp table, and then using a while to loop through each result. Because there are no ID's in the data, I can't keep count of the rows. Should I look at a SELECT TOP 1 query which deletes the row after processing to the Event viewer? Or is there an easier way?

Could someone point me in the right direction?
PS: This should work both on SQL 2008 R2 and SQL 2012
A CURSOR with the above query as it's definition would allow you to insert the records into the event log one-by-one. 

Another option you may want to look at is querying sys.dm_exec_query_stats:

SELECT TOP 10
    st.text,
    qs.max_logical_writes,
    qs.max_logical_reads,
    qs.max_elapsed_time,
    deqp.query_plan
FROM
    sys.dm_exec_query_stats AS qs
    CROSS APPLY sys.dm_exec_sql_text( qs.sql_handle ) AS st
    CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS deqp
ORDER BY
    qs.max_elapsed_time DESC


This query will return the top 10 longest running queries along with their plans. This is just a sample, it could easily be tweaked, and may point you in another direction.