gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:25:45 24 4
gpt4 key购买 nike

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

我正在尝试查看 sys.dm_exec_procedure_stats,但我没有运气:

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

最佳答案

我用过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

关于sql - 获取数据库中执行存储过程的用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54570169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com