• no need to reinvent the wheel. use sp_whoisactive, an absolutely amazing freebie from Adam Machanic. See sqlblog.com for code (which is documented) and 30 different blog posts detailing it's power.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service