Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting The Table Name of The Table That Fired A Trigger 1

Status
Not open for further replies.

webuser

MIS
Joined
Jun 1, 2001
Messages
202
Location
US
Is there a way to get the table name of the table that fired a trigger from WITHIN that trigger? In other words, say I have a table 'Employees' and a trigger on it defined below:

ALTER TRIGGER xyz ON dbo.[MySchedTable]
FOR INSERT, UPDATE, DELETE
NOT FOR REPLICATION
AS

DECLARE @NumInsertedRecords int
...
...

I want to be able to know within the trigger what my table name is, in this case 'MySchedTable'. Is there any way to do this?

Thanks!


 
Does anyone know how to do this?
 

I don't know of an existing function that will get the table name but this SQL script should work. The code selects the object name of the parent of the trigger from sysobjects.

DECLARE @ObjectName sysname
-- Seelct table name from sysobjects
SELECT @ObjectName = OBJECT_NAME(parent_obj)
FROM sysobjects (NOLOCK)
-- @@PROCID is the ID of the trigger
WHERE id = @@PROCID Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
 
Unbelievable! This is great. Simple and works perfectly. Thanks Again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top