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.

3
  • 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. Commented Oct 7, 2022 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. Commented Oct 7, 2022 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. Commented Oct 7, 2022 at 23:23

1 Answer 1

3

You Can't Do That™.

Triggers need hard coded table names.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.