我正在尝试将以下触发器从PL/SQL转换为MySQL
尤其是我想知道怎么做
1. FOR quantity in 1..:new.product_quantity
2. FOR row IN ()。
create or replace trigger "TRG_INSERT_BILL_PRODUCTS"
after insert on Bill_Products
for each row
begin
FOR quantity in 1..:new.product_quantity
LOOP
FOR row IN (
SELECT pa.article_id,pa.consist_quantity
FROM product_articles pa
WHERE pa.product_id=:new.product_id)
LOOP
update store
set store_quantity=store_quantity-row.consist_quantity
where article_id=row.article_id;
END LOOP;
END LOOP;
END;触发器说明:存储表有store.article_id和store.store_quantity,Product_articles表有pa.product_id、pa.article_id (产品中一致的文章)、pa.consist_quantity (文章)
因此,在将产品插入到账单中之后,我希望找到他的所有包含的文章,并降低store.article_id的store.article_id,即product_quantity (该产品中添加了多少产品)* consist_quantity (产品中的产品)。
发布于 2014-02-20 01:16:50
1..:new.product_quantity中的数量
MySql没有FOR循环。
您可以使用WHILE循环来模拟它:
Set quantity = 1
WHILE quantity <= :new.product_quantity DO
.....
statement_list
.....
Set quantity = quantity + 1
END WHILE对于行(查询)循环..。
MySql不支持这种循环,您必须声明一个游标并处理它:
DECLARE cursor_name CURSOR FOR
SELECT pa.article_id,pa.consist_quantity
FROM product_articles pa
WHERE pa.product_id=:new.product_id;还为该游标声明一个继续处理程序,然后明确地打开游标,在循环中从游标中获取行并关闭它。
请参阅文件:http://dev.mysql.com/doc/refman/5.6/en/cursors.html
了解如何使用MySql游标和示例。
https://stackoverflow.com/questions/21895887
复制相似问题