查询 SQL 服务器中的服务器级触发器定义

Querying a server level trigger definition in SQL Server

我正在尝试查询服务器级触发器的定义文本。更具体地说,尝试从用户定义的存储过程中查询定义。因为触发器保存在服务器级别,所以正常的方法似乎不起作用。

我试过的:

1) sp_helptext:范围错误,我收到消息:

Msg 15009, Level 16, State 1, Procedure sp_helptext, Line 54 The object 'myTrigger' does not exist in database 'master' or is invalid for this operation.


2) SELECT OBJECT_DEFINITION (OBJECT_ID(N'myTrigger)) AS ObjectDefinition;:

这个returnsNULL。我用 sys.server_triggers 中的对象 ID 替换了 "myTrigger"。这也returnsNULL.


3)

SELECT
    name,
    s.Definition
FROM 
    sys.server_triggers t
    INNER JOIN sys.sql_modules  s
        ON t.object_id = s.object_id
WHERE name = 'myTrigger'

对象不存在于sys.sql_modules

试试这个

SELECT ssmod.definition AS [Definition]
FROM master.sys.server_triggers AS tr
LEFT OUTER JOIN master.sys.server_assembly_modules AS mod ON mod.object_id = tr.object_id
LEFT OUTER JOIN sys.server_sql_modules AS ssmod ON ssmod.object_id = tr.object_id
WHERE (tr.parent_class = 100) and (tr.name = 'MyTriggerName')