30.06.2013 Views

Performance Tuning with SQL Server Dynamic Management Views

Performance Tuning with SQL Server Dynamic Management Views

Performance Tuning with SQL Server Dynamic Management Views

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 2: Connections, Sessions and Requests<br />

SELECT der.statement_start_offset ,<br />

der.statement_end_offset ,<br />

SUBSTRING(dest.text, der.statement_start_offset / 2,<br />

( CASE WHEN der.statement_end_offset = -1<br />

THEN DATALENGTH(dest.text)<br />

ELSE der.statement_end_offset<br />

END - der.statement_start_offset ) / 2)<br />

AS statement_executing ,<br />

dest.text AS [full statement code]<br />

FROM sys.dm_exec_requests der<br />

INNER JOIN sys.dm_exec_sessions des<br />

ON des.session_id = der.session_id<br />

CROSS APPLY sys.dm_exec_sql_text(der.sql_handle) dest<br />

WHERE des.is_user_process = 1<br />

AND der.session_id @@spid<br />

ORDER BY der.session_id ;<br />

Listing 2.14: Parsing the <strong>SQL</strong> text using statement_start_offset and statement_end_offset.<br />

If you change the "" to "=" in the WHERE clause and run Listing 2.14, you'll simply get<br />

back the text of that query, <strong>with</strong> 0 for the start offset and -1 for the end offset. If you want<br />

to really see these offsets in action, open a tab in SSMS and run the query in Listing 2.15.<br />

WAITFOR DELAY '00:01' ;<br />

BEGIN TRANSACTION<br />

-- WAITFOR DELAY '00:01' ;<br />

INSERT INTO AdventureWorks.Production.ProductCategory<br />

( Name, ModifiedDate )<br />

VALUES ( 'Reflectors', GETDATE() )<br />

ROLLBACK TRANSACTION<br />

SELECT Name ,<br />

ModifiedDate<br />

FROM AdventureWorks.Production.ProductCategory<br />

WHERE Name = 'Reflectors' ;<br />

-- WAITFOR DELAY '00:01' ;<br />

Listing 2.15: Investigating offsets.<br />

63

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!