获取当前更新的列名称以在触发器中使用
有没有办法实际获取已更新的列名称以便在触发器中使用它?
基本上,每当用户插入或更新表时,我都会尝试进行审计跟踪(在本例中,它与联系人表有关)
CREATE TRIGGER `after_update_contact`
AFTER UPDATE ON `contact` FOR EACH ROW
BEGIN
INSERT INTO user_audit (id_user, even_date, table_name, record_id, field_name, old_value, new_value)
VALUES (NEW.updatedby, NEW.lastUpdate, 'contact', NEW.id_contact, [...])
END
如何获取已更新的列的名称,并从中获取OLD 和
NEW
值。如果连续更新多列甚至多行,是否可以对每次更新进行审核?
Is there a way to actually get the column name that was updated in order to use it in a trigger?
Basically I'm trying to have an audit trail whenever a user inserts or updates a table (in this case it has to do with a Contact table)
CREATE TRIGGER `after_update_contact`
AFTER UPDATE ON `contact` FOR EACH ROW
BEGIN
INSERT INTO user_audit (id_user, even_date, table_name, record_id, field_name, old_value, new_value)
VALUES (NEW.updatedby, NEW.lastUpdate, 'contact', NEW.id_contact, [...])
END
How can I get the name of the column that's been updated and from that get the OLD
and NEW
values of that column. If multiple columns have been updated in a row or even multiple rows would it be possible to have an audit for each update?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用 OLD.colname <>每列的 NEW.colname 来检查并查找不同的列。不幸的是,触发器在 MySQL 中的使用有点受限。
Just use OLD.colname <> NEW.colname for each column to check and find those that are different. Triggers are a bit limited in their use in MySQL, too bad.
试试这个代码...
Try this code...