I generally don't get into the technical side of GP in the Tip42day but this is just too good not to share.
A user on the https://community.dynamics.com/forums/32.aspx forum posed the following question -
Is there any column that will show the last activity time for a user? I would like to be able to send email alerts to users who are logged in but haven't been active for 1 hour.
This is a great idea but I'm not aware of any field in any table in GP that stores this info. However, I didn't think about pulling this directly from SQL. Fortunately, there are people much smarter than I am out there who did. Here's the solution -
SELECT CASE WHEN S.session_id IS NULL THEN 'Missing DEX_SESSION'
ELSE ''
END MISSING_SESSION,
CASE WHEN DATEDIFF(mi, P.last_batch, GETDATE()) > 1
THEN 'Idle for ' + LTRIM(RTRIM(STR(DATEDIFF(mi, P.last_batch, GETDATE())))) + ' minutes.'
ELSE ''
END AS IDLE_TIME_DESC,
CASE WHEN DATEDIFF(mi, P.last_batch, GETDATE()) > 1
THEN DATEDIFF(mi, P.last_batch, GETDATE())
ELSE 0
END AS IDLE_TIME,
A.USERID,
A.CMPNYNAM COMPANY_NAME,
INTERID COMPANY_ID,
LOGINDAT + LOGINTIM LOGIN_DATE_TIME,
SQLSESID SQL_SESSIONID,
P.login_time SQL_LOGINTIME,
P.last_batch SQL_LAST_BATCH,
DATEDIFF(mi, P.last_batch, GETDATE()) TIME_SINCE_LAST_ACTION,
S.session_id SQLSERVER_SESSIONID,
S.sqlsvr_spid SQLSERVER_PROCESSID,
P.spid PROCESSID,
P.status PROCESS_STATUS,
P.net_address NET_ADDRESS,
P.dbid DATABASE_ID,
P.hostname HOSTNAME
FROM DYNAMICS..ACTIVITY A
LEFT JOIN DYNAMICS..SY01400 U ON A.USERID = U.USERID
LEFT JOIN DYNAMICS..SY01500 C ON A.CMPNYNAM = C.CMPNYNAM
LEFT JOIN tempdb..DEX_SESSION S ON A.SQLSESID = S.session_id
LEFT JOIN master..sysprocesses P ON S.sqlsvr_spid = P.spid
AND ecid = 0
LEFT JOIN master..sysdatabases D ON P.dbid = D.dbid
This script creates a column called 'IDLE_TIME' and stores the idle time in minutes, which can be used to determine which users to send the email to via SQL Mail.
Thanks to Ron Wilson and Sivakumar Venkataraman for this great tip!
No comments:
Post a Comment