05 November 2008

How to get the last execution row in sql server and Cross apply


Have you ever think how to program better exception handling in your sql script, so this little article will teach how to do that, let's begin

The following query will give the last executed row in sql server

SELECT top 1 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


Notice : You can remove the top 1 and view all the last executed rows.

Cross & Apply Terms

The APPLY term let's you join a table to a table valued function.

No ON cluse used for this joining.

2 types of apply :

1. CROSS
Returns rows from the left side (Customers) if the table-valued-function returns rows.

2. OUTER
Returns all the rows on the left side (Customers) whether they return any rows in the table-valued-function or not. The columns that the table-valued-function returns are null if no rows are returned for the opposite row in the left side.