问题
Is there a way within a Sql Server 2005 Trigger to get the name and schema of the table that the trigger is attached to during execution?
回答1:
SELECT
OBJECT_NAME(parent_id) AS [Table],
OBJECT_NAME(object_id) AS TriggerName
FROM
sys.triggers
WHERE
object_id = @@PROCID
Then you can also use OBJECTPROPERTY to get extra info, such as after/before, delete/insert/update, first/last etc
回答2:
This is a dirty way to know it
SELECT o.name
FROM sysobjects t
JOIN sysobjects o ON t.parent_obj = o.id
WHERE t.name = 'your_trigger_name'
[EDIT]
According to the other answer and the comments, i think this can fit to you (MSSQL2000 version)
SELECT o.name
FROM sysobjects t
JOIN sysobjects o ON t.parent_obj = o.id
WHERE t.id = @@PROCID
来源:https://stackoverflow.com/questions/726993/sql-trigger-which-table-does-it-belong-to