1

I'm using Mysql 5.7 and I want to use triggers to set up some simple audit trail logging. The table name the entry comes from is relevant so we'd like to capture it. I know I can hard code the name here but I was hoping for something a little more dynamic and portable (copy and paste to another table). Is it possible to get the table name that a given trigger is associated while inside of the trigger?

Here is the guts of my trigger...

BEGIN
SET @table_name = `i_want_the_table_name_for_this_trigger`;

INSERT INTO `test`.`audit_table1` (`serialNumber`, `auditTrailTableName`, `action`, `communicationDate`) VALUES (OLD.id,  @table_name, NEW.type, NEW.createdOn);

END

I found this answer for SQL Server... How to get table_name in a trigger - SQL Server

Any help is appreciated.

kasdega
  • 18,396
  • 12
  • 45
  • 89
  • you can get the table name dynamically from information_schema through the name of the trigger. Unfortunately, you have to hard code the trigger name into the code, so this does not really help. – Shadow Oct 07 '22 at 22:30
  • ms sql server and mysql vastly differ from each other. Just because you can do a thing in one, it does not mean you can do it in the other. – Shadow Oct 07 '22 at 23:08
  • Thank you @Shadow for your response. I only meant that reference as an example of more precisely what I was hoping to be able to do. – kasdega Oct 07 '22 at 23:23

1 Answers1

3

You Can't Do That™.

Triggers need hard coded table names.

O. Jones
  • 103,626
  • 17
  • 118
  • 172