Q:

dynamic view management query

I want to apply the Table valued function sys.dm_exec_sql_text() for [sql_handle] in the below query.
SELECT  sql_handle
FROM    sys.dm_exec_query_stats A

To me the below 2 queries not working
Select  *,sys.dm_exec_sql_text(A.sql_handle)
from    sys.dm_exec_query_stats A 

Error:
Msg 4121, Level 16, State 1, Line 1
Cannot find either column "sys" or the user-defined function or aggregate "sys.dm_exec_sql_text", or the name is ambiguous.

Select  *
from sys.dm_exec_query_stats A,
sys.dm_exec_sql_text(A.sql_handle)

Error:
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "A.sql_handle" could not be bound.

A:

you can CROSS APPLY :
...
from sys.dm_exec_query_stats A           
CROSS APPLY sys.dm_exec_sql_text(A.sql_handle)

like the example from microsoft:
SELECT TOP 5 total_worker_time/execution_count AS [Avg CPU Time],
    SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,  
        ((CASE qs.statement_end_offset
          WHEN -1 THEN DATALENGTH(st.text) 
         ELSE  qs.statement_end_offset
         END  - qs.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
ORDER BY total_worker_time/execution_count DESC;

