title: How to get table_name in a trigger - SQL Server
body: <p>I've created a trigger which task is to store information about the trigger-events.
  For example: "New employee added to the table on date 2014-10-13. </p>
  
  <p>I've created a table - Audit - which stores all the information (from the trigger).</p>
  
  <pre><code>CREATE TABLE [dbo].[Audit](
  [id] [int] IDENTITY(1,1) NOT NULL,
  [tableName] [nvarchar](255) NOT NULL,
  [auditData] [nvarchar](max) NULL,
  [userName] [nvarchar](255) NOT NULL,
  PRIMARY KEY (id)) 
  </code></pre>
  
  <p><strong>However, the trigger I've created looks like this:</strong></p>
  
  <pre><code>CREATE TRIGGER [dbo].[tr_Actor_ForInsert_Audit]
  ON [dbo].[Actor]
  FOR INSERT
  AS
  BEGIN
  DECLARE @userName NVARCHAR(255)
  DECLARE @tableName NVARCHAR(255) = 'Actor'
  DECLARE @name VARCHAR(255)
  DECLARE @birthdate DATE
  
  SELECT @userName = SYSTEM_USER
  SELECT @name = name FROM inserted
  SELECT @birthdate = birthdate FROM inserted
  
  INSERT INTO Audit VALUES (@tableName, 'New ' + LOWER(@tableName) + ' with Name = ' + @name +
                           ' and Birthdate = ' + CONVERT(NVARCHAR,@birthdate) + ' was added at ' + CONVERT(NVARCHAR,GETDATE()), @userName)
  </code></pre>
  
  <p>END;</p>
  
  <p>As you can see, the variable <strong>userName</strong> is initialized to <strong>SYSTEM_USER.</strong>
  but the variable <strong>tableName</strong> is intitialized to a <strong>hard-coded value</strong> .</p>
  
  <p><strong>Question:
  Is there any possible way to somehow generically initialized the variable tableName to the tableName
  the same way I did for userName?</strong></p>
  
  <p>For example, if something like this existed:</p>
  
  <p>@tableName = SYSTEM_TABLE_WHERE_TRIGGER(TRIGGERNAME)_EXIST</p>
  
  <p>Regards,</p>
  
  <p>Christian</p>