mysql存储过程触发器_在MySQL中使用存储过程创建触发器

bd96500e110b49cbb3cd949968f18be7.png

Is it possible to build a stored procedure that creates a trigger in MySQL?

I have the stored proc below, which works fine, but it only outputs the code to create the trigger, but does not actually create it.

DELIMITER $

CREATE PROCEDURE addCustomerLogTrigger()

BEGIN

SELECT CONCAT(

'CREATE TRIGGER customer_audit AFTER UPDATE ON customer FOR EACH ROW BEGIN ',

GROUP_CONCAT(

CONCAT(

'IF OLD.', column_name, ' != NEW.', column_name, ' THEN INSERT INTO st_person_audit_log (',

'id, ',

'fieldName, ',

'old_value, ',

'new_value, ',

'time_stamp, ',

'person_id'

') VALUES (

NEW.id,

''', column_name, ''',

OLD.', column_name, ',

NEW.', column_name, ',

NOW(),

NEW.last_updated_by

); END IF;'

)

SEPARATOR ' '

), ' END;$'

)

FROM

information_schema.columns

WHERE

table_schema = database()

AND table_name = 'customer';

END$

DELIMITER ;

Is there a way to create it in one go?

I got it from http://thenoyes.com/littlenoise/?p=43 and adjusted it a little to my needs.

Just FYI, this trigger inserts a old/new value pair into an audit table, whenever something changes in the customer table.

解决方案

No, it is not possible, even with MySQL prepared statements.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值