Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get table_name in a trigger - SQL Server

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.

I've created a table - Audit - which stores all the information (from the trigger).

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)) 

However, the trigger I've created looks like this:

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)

END;

As you can see, the variable userName is initialized to SYSTEM_USER. but the variable tableName is intitialized to a hard-coded value .

Question: Is there any possible way to somehow generically initialized the variable tableName to the tableName the same way I did for userName?

For example, if something like this existed:

@tableName = SYSTEM_TABLE_WHERE_TRIGGER(TRIGGERNAME)_EXIST

Regards,

Christian

like image 224
ChrisRun Avatar asked Oct 13 '14 08:10

ChrisRun


People also ask

How do I get the insert row in a trigger?

To create a trigger, we need to change the delimiter. Inserting the row into Table1 activates the trigger and inserts the records into Table2. To insert record in Table1. To check if the records are inserted in both tables or not.

How can you tell what table a trigger is on?

Just go to your table name and expand the Triggers node to view a list of triggers associated with that table. Right click to modify your trigger.

Can a trigger include select statement?

The trigger event that initiates the trigger action can be an INSERT, DELETE, UPDATE, or a SELECT statement.

How do I get a list of triggers in SQL Server?

To view database level triggers, Login to the server using SQL Server management studio and navigate to the database. Expand the database and navigate to Programmability -> Database Triggers. To view triggers at the server level, Login to Server using SSMS and navigate to Server Objects and then Triggers folder.


1 Answers

I tweaked @ChrisRun answer a little bit to include schema of the trigger in case You would have same trigger names in different schemas.

@tablename = SCHEMA_NAME(schema_id) + '.' + OBJECT_NAME(parent_object_id)
         FROM sys.objects 
         WHERE sys.objects.name = OBJECT_NAME(@@PROCID)
            AND SCHEMA_NAME(sys.objects.schema_id) = OBJECT_SCHEMA_NAME(@@PROCID)

All the best, Mike

like image 164
Michal L. Avatar answered Sep 20 '22 00:09

Michal L.