'MYSQL Audit Trail
Hello I use the following way to use an audit trail:
First a user log into a database with the following procedure:
CREATE DEFINER=`root`@`%` PROCEDURE `oms_Insert_AuditTrailLogin`(
IN `EmployeeRefID` BIGINT,
IN `IPAdress` VARCHAR(50),
IN `LoginTime` DATETIME
)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
SET @EmployeeID = EmployeeRefID;
INSERT INTO omsdb.tbl_data_AuditTrailLogin
(EmployeeRefID, IPAdress, LoginTime)
VALUES
(EmployeeRefID, IPAdress, LoginTime);
SELECT Last_Insert_ID() AS ID;
END
I need the variable @EmployeeID to identify the user.
After update trigger:
...
INSERT INTO omsdb.tbl_data_AuditTrail
(evType, Tablename, DatabaseName, PrimaryKey, UpdateDate, @EmployeeID)
VALUES
('UPDATE', 'tbl_data_bcrabl', 'molbiolabdb', @Pk, NOW(), @EmployeeID);
SET @AuditTrailRefID = LAST_INSERT_ID();
IF NEW.bcrabl != OLD.bcrabl OR OLD.bcrabl IS NULL THEN INSERT INTO omsdb.tbl_data_AuditTrailData
(AuditTrailRefID, FieldName, OldValue, NewValue)
VALUES
(@AuditTrailRefID, 'bcrabl', OLD.bcrabl, NEW.bcrabl);
END IF;
...
the after update trigger works fine but the @EmployeeID has no value - it's always "NULL" How could I create variables outside a trigger and use this information in the trigger to identify the user? I could not use the function USER() etc.
Thank you very much
Elmar
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
