Sql-Server
查看特定使用者執行的腳本?
我在 stackoverflow 文章中讀到,您可以使用以下查詢恢復過去 24 小時內的腳本:
Use <database> SELECT execquery.last_execution_time AS [Date Time], execsql.text AS [Script] FROM sys.dm_exec_query_stats AS execquery CROSS APPLY sys.dm_exec_sql_text(execquery.sql_handle) AS execsql ORDER BY execquery.last_execution_time DESC
但我只想查看特定使用者執行的查詢,我該如何執行此操作?
為了實現它,您需要在執行時使用 DMV 或擴展事件來擷取此資訊。
以下查詢將為您提供幫助:
SELECT cr.DatabaseName ,s.session_id ,s.host_name ,s.program_name ,s.client_interface_name ,s.login_name ,s.login_time ,s.nt_domain ,s.nt_user_name ,c.client_net_address ,c.local_net_address ,cr.ObjName ,cr.Query FROM sys.dm_exec_sessions AS s INNER JOIN sys.dm_exec_connections AS c ON c.session_id = s.session_id CROSS APPLY ( SELECT db_name(dbid) AS DatabaseName ,object_id(objectid) AS ObjName ,ISNULL(( SELECT TEXT AS [processing-instruction(definition)] FROM sys.dm_exec_sql_text(c.most_recent_sql_handle) FOR XML PATH('') ,TYPE ), '') AS Query FROM sys.dm_exec_sql_text(c.most_recent_sql_handle) ) cr where s.nt_user_name = '' -- filter here your user name and s.session_id <> @@SPID ORDER BY c.session_id