If the list things to check is constant, then spell them out rather than constructing the tests. (not viable)
But, presumably that is not the case...
Plan A: Replace the INSERT
that needs to call the trigger with a PROCEDURE
call. The routine contains the INSERT
plus the rest of the code you presented.
Plan B: As with Plan A, but do it in application code.
Note: Either A or B would be wrapped in BEGIN
...COMMIT
to make it 'atomic'.
Bottom Line: Triggers can't do everything.
Enforcement of A (I think this will prevent other from bypassing the stored proc.)
CREATE USER special@... ...
-- a new userGRANT INSERT ON db.tbl TO special@...
-- let it get in- The stored proc would be
SECURITY special@...
-- to run proc as "special" so it can do theINSERTs
- Remove
INSERT PRIVILEGE
for that table from others. (This gets messy, or maybe unnecessary.) (It may help if the table is in a separate database that no one currently has access to.) - (My gut says there is one more step. I'll leave it as a 'exercise for the reader'.)