在下面的触发器中的delete和insert,需要写一个事务,本人对mysql存储过程的事务不熟,还请各位高手请教,有点急,非常感谢
DROP TRIGGER IF EXISTS trigger_products_data_update;
CREATE TRIGGER trigger_products_data_update
AFTER UPDATE ON erp_products_data
FOR EACH ROW
BEGIN
declare n ,num int;
set n=1;
set num=char_length(new.products_suppliers_ids)-char_length(replace(new.products_suppliers_ids,”,”,””))+1;
DELETE from erp_products_suppliers WHERE sku=new.products_sku;
while (n<=num)
do
insert into erp_products_suppliers(sku,suppliers_ids) values(new.products_sku,SUBSTRING_INDEX(SUBSTRING_INDEX(new.products_suppliers_ids,”,”,n),”,”,-1));
set n=n+1;
end while;
END;
DROP TRIGGER IF EXISTS trigger_products_data_update;
CREATE TRIGGER trigger_products_data_update
AFTER UPDATE ON erp_products_data
FOR EACH ROW
BEGIN
declare n ,num int;
set n=1;
set num=char_length(new.products_suppliers_ids)-char_length(replace(new.products_suppliers_ids,”,”,””))+1;
DELETE from erp_products_suppliers WHERE sku=new.products_sku;
while (n<=num)
do
insert into erp_products_suppliers(sku,suppliers_ids) values(new.products_sku,SUBSTRING_INDEX(SUBSTRING_INDEX(new.products_suppliers_ids,”,”,n),”,”,-1));
set n=n+1;
end while;
END;
解决方案
40
DROP TRIGGER IF EXISTS trigger_products_data_update;
CREATE TRIGGER trigger_products_data_update
AFTER UPDATE ON erp_products_data
FOR EACH ROW
BEGIN
declare n ,num int;
set n=1;
set num=char_length(new.products_suppliers_ids)-char_length(replace(new.products_suppliers_ids,”,”,””))+1;
start transaction;
DELETE from erp_products_suppliers WHERE sku=new.products_sku;
while (n<=num)
do
insert into erp_products_suppliers(sku,suppliers_ids) values(new.products_sku,SUBSTRING_INDEX(SUBSTRING_INDEX(new.products_suppliers_ids,”,”,n),”,”,-1));
set n=n+1;
commit;
end while;
END;
CREATE TRIGGER trigger_products_data_update
AFTER UPDATE ON erp_products_data
FOR EACH ROW
BEGIN
declare n ,num int;
set n=1;
set num=char_length(new.products_suppliers_ids)-char_length(replace(new.products_suppliers_ids,”,”,””))+1;
start transaction;
DELETE from erp_products_suppliers WHERE sku=new.products_sku;
while (n<=num)
do
insert into erp_products_suppliers(sku,suppliers_ids) values(new.products_sku,SUBSTRING_INDEX(SUBSTRING_INDEX(new.products_suppliers_ids,”,”,n),”,”,-1));
set n=n+1;
commit;
end while;
END;
40