获取谁在数据库中执行存储过程的用户名?

时间:2019-02-07 09:32:18

标签: sql sql-server stored-procedures sql-server-2016

有人在他的机器上运行了一个存储过程,我只是想知道谁执行了该存储过程。

我试图查看sys.dm_exec_procedure_stats,但是我没有运气:

SELECT object_id
FROM sys.dm_exec_procedure_stats
WHERE OBJECT_NAME(object_id,database_id) = 'SpName' 

1 个答案:

答案 0 :(得分:0)

我已经使用https://dba.stackexchange.com/questions/135078/how-to-get-history-of-queries-executed-with-username-in-sql并对其逻辑进行了更新,并且逻辑对我有用

USE master
go
SELECT top 10 sdest.DatabaseName 
    ,sdes.session_id
    ,sdes.[host_name]
    ,sdes.[program_name]
    ,sdes.client_interface_name
    ,sdes.login_name
    ,sdes.login_time
    ,sdes.nt_domain
    ,sdes.nt_user_name
    ,sdec.client_net_address
    ,sdec.local_net_address
    ,sdest.ObjName
    ,sdest.Query
FROM sys.dm_exec_sessions AS sdes
INNER JOIN sys.dm_exec_connections AS sdec ON sdec.session_id = sdes.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(sdec.most_recent_sql_handle)
                FOR XML PATH('')
                    ,TYPE
                ), '') AS Query

    FROM sys.dm_exec_sql_text(sdec.most_recent_sql_handle) WHERE OBJECT_NAME(objectid,dbid)='MySp' 
    ) sdest
where sdes.session_id <> @@SPID  
--and sdes.nt_user_name = '' -- Put the username here !
ORDER BY sdec.session_id