2014-02-20 21 views
1

我試圖從PL/SQL中使用以下觸發轉換到MySQLPL/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表已經store.article_id和該文章 Product_articles表store.store_quantity已經pa.product_id,pa.article_id(文章是一致的產品),pa.consist_quantity (的文章)

所以在插入一個產品的帳單後,我想找到他所有的文章,並降低store.article_id store.article_id,這將是product_quantity(多少產品的增加該法案)* consist_quantity(產品中的那篇文章)

回答

2

求量在1 ..:new.product_quantity

MySQL有沒有FOR循環。

Set quantity = 1 
WHILE quantity <= :new.product_quantity DO 
    ..... 
    statement_list 
    ..... 
    Set quantity = quantity + 1 
END WHILE 



FOR行(查詢)LOOP ...

MySQL不支持這樣的:
可以使用WHILE循環效仿循環,您必須聲明一個遊標並對其進行處理:

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遊標。

+0

非常感謝! – jovanMeshkov